Do closures in Javascript get a reference to the full outer function or do they only get references to the variables they actually use? -


just out of curiosity, closures in javascript reference whole "outer environment", or returned function analyzed see variables in outer scope references , gets references those?

theoretically, nested function in javascript has access variables in containing scopes. when identifier encountered, resolved against scope chain, list includes objects properties variables , function parameters of each containing execution context (i.e. enclosing function), innermost first, plus global object @ end. function object drags scope chain around wherever goes.

however, these variable objects , scope chain specification constructs , not accessible directly, implementations free make whatever optimizations like, including analyzing function code , exposing variables accessed function , functions nested within it, long specification appears satisfied. however, it's best assume if have enormous object accessible via closure function, enormous object going stick around @ least until function garbage collected.

if want further information this, read ecmascript specification. starting point section 10.1.4: http://bclary.com/2004/11/07/#a-10.1.4. not current version of specification baseline current major browsers implement.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -