jquery – scope inside $(document).ready()?

Javascript uses functional scopes, so local variables inside a function are not visible to the outside. This is why your code can’t access code from other scopes.

The ideal solution to this is create a Namespace.

var NS = {};

(function(){
  function privateFunction() { ... }
  NS.publicFunction = function(){ ... }
})();

$(document).ready(function(){
  NS.publicFunction();
});

This is also a useful pattern because it allows you to make a distinction between private & public elements.

Leave a Comment