Are closures in javascript recompiled
Posted
by Discodancer
on Stack Overflow
See other posts from Stack Overflow
or by Discodancer
Published on 2010-05-10T20:42:46Z
Indexed on
2010/05/10
20:54 UTC
Read the original article
Hit count: 249
Let's say we have this code (forget about prototypes for a moment):
function A(){
var foo = 1;
this.method = function(){
return foo;
}
}
var a = new A();
is the inner function recompiled each time the function A is run? Or is it better (and why) to do it like this:
function method = function(){ return this.foo; }
function A(){
this.foo = 1;
this.method = method;
}
var a = new A();
Or are the javascript engines smart enough not to create a new 'method' function every time? Specifically Google's v8 and node.js.
Also, any general recommendations on when to use which technique are welcome. In my specific example, it really suits me to use the first example, but I know thath the outer function will be instantiated many times.
© Stack Overflow or respective owner