Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference in closure style

Tags:

javascript

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?

like image 436
doekman Avatar asked Aug 08 '08 20:08

doekman


People also ask

Which closure is the best?

The best closure is the closure that looks most natural on you. Try both to be sure you know which one you're most comfortable with or allow a professional to conduct a brief consultation to see which closure will be best considering your hairline, head-shape, lifestyle, and the style you're looking to achieve.

What is the most natural looking closure?

Silk Closures: Our Silk Base Closure (Size 4x4") is the most natural looking closure on the market. Silk Base Front Closure is a Closure that is worn with a weave to give a natural looking hairline; the Silk Base material creates a scalp for this closure, and therefore can be parted and styled in any way.

What's better a frontal or closure?

A lace frontal or a lace closure? The truth is, both are great. Both will give you a full and flawless install with the illusion that hair is growing directly from your scalp. Closures and Frontals allow you to braid and protect your natural hair.


3 Answers

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

like image 115
olliej Avatar answered Oct 20 '22 13:10

olliej


@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 ;-)

like image 24
doekman Avatar answered Oct 20 '22 13:10

doekman


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.

like image 3
Kieron Avatar answered Oct 20 '22 14:10

Kieron