September 10, 2013

The need for JS functions which are self executing?

Bhb’s Question:

What is the use of functions which self execute?

(function(w,d) {...})(window, document);

And why do they pass window/document which are global functions?

Why do they pass window/document which are global functions?

So that inside that function scope window and document will be available as w and d.

One of the common practice of this is included with jQuery plugin development as:

(function($) {
   //...
})(jQuery); 

Since $ can be used in prototype too, this can create anamolities when mixing two libraries, but inside above function, $ will only represent jQuery but not prototype.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!