There's a common pattern of "forking" a returning function as in the
following example:
function bind(fn, context) {
var args = Array.prototype .slice.call(arg uments, 2);
if (args.length) {
return function() {
fn.apply(contex t, args);
}
}
return function() {
return fn.call(context );
}
}
The runtime speed benefits are obvious, but I've been told that there
is an increased memory consumption in such cases. Are there 2 Function
objects created when `bind` is being called? I assume that's not the
case, since those are not FunctionDeclara tion's, but rather
FunctionExpress ion's (and so they should not be evaluated foremost
when execution context is entered). Are FunctionExpress ion's contained
within blocks that are never evaluated create Function objects? Does
it make a difference if FunctionExpress ion is contained within a
`return` clause?
I can't find relevant parts in the specification and would appreciate
any insights on this matter.
--
kangax
following example:
function bind(fn, context) {
var args = Array.prototype .slice.call(arg uments, 2);
if (args.length) {
return function() {
fn.apply(contex t, args);
}
}
return function() {
return fn.call(context );
}
}
The runtime speed benefits are obvious, but I've been told that there
is an increased memory consumption in such cases. Are there 2 Function
objects created when `bind` is being called? I assume that's not the
case, since those are not FunctionDeclara tion's, but rather
FunctionExpress ion's (and so they should not be evaluated foremost
when execution context is entered). Are FunctionExpress ion's contained
within blocks that are never evaluated create Function objects? Does
it make a difference if FunctionExpress ion is contained within a
`return` clause?
I can't find relevant parts in the specification and would appreciate
any insights on this matter.
--
kangax
Comment