Consider the following code:
(function($) {
function hello(who)
{
console.log('Hello: '+who);
}
})(jQuery);
$(document).ready(function() {
hello('World');
});
How would it be possible to access the hello function from outside the anonymous function?
Assuming you’re going to want to eventually add other methods to this closure as well, you could create a namespace using the module pattern:
Here the methods and properties are just to show examples of ways to declare things privately and publicly within the closure. For illustration:
Also, I used the
$argument in the self-executing function so thatjQueryis passed to something, you’d use$internally to accessjQuery.