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?
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.
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.
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.
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 expression
s prototype, eg. (before the expression.call
) tempObject.__proto__ = expression.prototype
@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 ;-)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With