tags:

views:

322

answers:

6

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?

+1  A: 

The second example will execute the function after creating it.

edit: this isn't really true.

Lance Fisher
+1  A: 

@Lance: the first one is also executing. Compare it with a named constructor:

function Blah() {
alert('blah');
}
new Bla();

this is actually also executing code. The same goes for the anonymous constructor...

But that was not the question ;-)

doekman
+4  A: 

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

olliej
A: 

Well, I made a page like this:

<html>
<body>
<script type="text/javascript">
var a = new function() {
alert("method 1");

return "test";
};

var b = (function() {
alert("method 2");

return "test";
})();

alert(a); //a is a function
alert(b); //b is a string containing "test"

</script>
</body>
</html>

Surprisingly enough (to me anyway) it alerted both "method 1" and method 2". I didn't expect "method 1" to be alerted. The difference was what the values of a and b were. a was the function itself, while b was the string that the function returned.

Lance Fisher
A: 

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.

Example:

<html>
<body>
<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");
</script>
</body>
</html>

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!

Adhip Gupta
-1 This is due to omitting "var" before "a" making it global and is unrelated to the question.
Tomas
A: 

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.

Kieron