Before I heard about self executing functions I always used to do this:
$(document).ready(function() {
doSomething();
});
function doSomething()
{
// blah
}
Would a self executing function have the same effect? will it run on dom ready?
(function doSomething($) {
// blah
})(jQuery);
Nope. A self executing function runs when the Javascript engine finds it.
However, if you put all of you code at the end of your document before the closing
</body>tag (which is highly recommended), then you don’t have to wait for DOM ready, as you’re past that automatically.If all you want is to scope your
$variable, and you don’t want to move your code to the bottom of the page, you can use this: