0

I have started learning Jquery and I am struck with something. I searched to learn about it but I am rather confused

 (function($){
    //all code here
    })(window.jquery)

Some documents say that this code executes immediately.
Some say that it isolates the variables from the external world.
and some say that it is used to prevent '$'so that it could be used as a jquery object.

So what exactly does it do ?? Is there anything else that is done by this small arrogant code?

Let me see
  • 5,063
  • 9
  • 34
  • 47
  • 1
    `immediately invoked function expression` this suits. – Praveen Nov 13 '13 at 07:03
  • 2
    this might help you http://stackoverflow.com/questions/10896749/what-does-function-function-window-jquery-do – rajesh kakawat Nov 13 '13 at 07:04
  • 1
    see http://stackoverflow.com/questions/10896749/what-does-function-function-window-jquery-do see the real use that twitter uses using this – Dev Nov 13 '13 at 07:06

2 Answers2

1
(function () {
})()

Is a self-invoking anonymous function.

function ($) {

}(window.jQuery) 

Passing, window.jQuery into that function as argument and accepting as $.

What this does is making $ an alias to window.jQuery (original jQuery Object) and hence ensuring that the $ will always refer to the jQuery object inside that closure, no matter if other library has taken that($) outside.

Check this link : Link

Community
  • 1
  • 1
Ankit Tyagi
  • 2,381
  • 10
  • 19