1

What kind of a design pattern is this and what is the significance of using this closure?

(function(){
    // my code here
}).call(this); 

Edit

Then what is the difference between the above code and the following as the this keyword will still refer to the same object in both the ways.

(function(){
    // my code here
})(); 
Atif
  • 10,623
  • 20
  • 63
  • 96

1 Answers1

7

Thats an immediately invoked function expression.

More info here: http://benalman.com/news/2010/11/immediately-invoked-function-expression/

The purpose is to run code immediately while protecting scope (so variables declared within don't leak out to the global scope.

Update

call sets the value of this for the function its being applied on. Without it, the value is set to the window object, with it there it is set to the outer scope.

Ben McCormick
  • 25,260
  • 12
  • 52
  • 71