Are closures in javascript recompiled
- by Discodancer
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.