What's the difference in closure style

Tags: javascript
By : doekman
Source: Stackoverflow.com

There are two popular closure styles in javascript. The first I call anonymous constructor:

new function() { 
  var code...

and the inline executed function:

(function() {
  var code...

are there differences in behaviour between those two? Is one "better" over the other?

By : doekman


Yes, there are differences between the two.

Both are anonymous functions and execute in the exact same way. But, the difference between the two is that in the second case scope of the variables is restricted to the anonymous function itself. There is no chance of accidentally adding variables to the global scope.

This implies that by using the second method, you are not cluttering up the global variables scope which is good as these global variable values can interfere with some other global variables that you may use in some other library or are being used in a third party library.


<script type="text/javascript">

new function() { 
a = "Hello";
alert(a + " Inside Function");

alert(a + " Outside Function");

(function() { 
var b = "World";
alert(b + " Inside Function");

alert(b + " Outside Function");

In the above code the output is something like:

Hello Inside Function
Hello Outside Function
World Inside Function

... then, you get an error as 'b' is not defined outside the function!

Thus, I believe that the second method is better... safer!

They both create a closure by executing the code block. As a matter of style I much prefer the second for a couple of reasons:

It's not immediately obvious by glancing at the first that the code will actually be executed; the line looks like it is creating a new function, rather than executing it as a constructor, but that's not what's actually happening. Avoid code that doesn't do what it looks like it's doing!

Also the (function(){ ... })(); make nice bookend tokens so that you can immediately see that you're entering and leaving a closure scope. This is good because it alerts the programmer reading it to the scope change, and is especially useful if you're doing some postprocessing of the file, eg for minification.

By : Kieron

Both cases will execute the function, the only real difference is what the return value of the expression may be, and what the value of "this" will be inside the function.

Basically behaviour of

new expression

Is effectively equivalent to

var tempObject = {};
var result = expression.call(tempObject);
if (result is not an object)
result = tempObject;

Although of course tempObject and result are transient values you can never see (they're implementation details in the interpreter), and there is no JS mechanism to do the "is not an object" check.

Broadly speaking the "new function() { .. }" method will be slower due to the need to create the this object for the constructor.

That said this should be not be a real difference as object allocation is not slow, and you shouldn't be using such code in hot code (due to the cost of creating the function object and associated closure).

Edit: one thing i realised that i missed from this is that the tempObject will get expressions prototype, eg. (before the expression.call) tempObject.__proto__ = expression.prototype

By : olliej

This video can help you solving your question :)
By: admin