c# - Do variables need to be referenced for them to be included in a closure? -


when creating closure (in javascript or c#), variables in scope @ time of closure's creation "enclosed" in it? or variables referenced in newly created method?

example c# code:

private void main() {     var referenced = 1;     var notreferenced = 2;  // enclosed?     new int[1].select(x => referenced); } 

example javascript code:

    var referenced = 1;     var notreferenced = 2;  // enclosed?     var func = function () {         alert(referenced);     } 

(question came me when reading memory leaks in ie creating circular references javascript closures. http://jibbering.com/faq/notes/closures/#clmem)
note: word "enclosed", mean msdn call "captured". (http://msdn.microsoft.com/en-us/library/bb397687.aspx)

you have 2 questions here. in future might consider posting 2 separate questions when have 2 questions.

when creating closure in javascript, variables in scope @ time of closure's creation "enclosed" in it?

you not state of many versions of "javascript" talking about. i'm going assume talking correct implementation of ecmascript 3 language. if talking other version of "javascript", 1 you're talking about.

ecmascript 5 has updated rules on how lexical environments , "eval" work. have not been member of technical committee 39 since 2001, , have not been keeping up-to-date recent changes ecmascript 5 spec; if want answer in context of recent ecmascript 5 rules, find expert on specification; i'm not it.

the answer question in context of ecmascript 3 yes. ecmascript 3 specification clear on point, don't need @ spec know must case:

function f() {  var referenced = 1;  var notreferenced = 2;  var func = function ()   {    alert(referenced);    alert(eval("notreferenced"));  }  return func; } f()(); 

how can "eval" work correctly if "notreferenced" not captured?

this explained in ecmascript specification, can briefly sum here.

every variable associated "variable object", object has properties names names of variables. variable object function f identical activation object of f -- is, object magically created every time f invoked. variable object has 3 properties: "referenced", "notreferenced" , "func".

there variable object called "global object" represents code outside of function. has property "f".

every execution context has scope chain, list of objects properties searched when trying evaluate identifier.

every function object has scope chain associated it, copy of scope chain in effect when function created.

the scope chain associated "f" global object.

when execution enters "f", current scope chain of execution context has activation object of "f" pushed onto it.

when "f" creates function object assigned "func", associated scope chain copy of current scope chain of execution context -- is, scope chain contains activation of "f", , global object.

ok, objects set correctly. f returns func, invoked. creates activation object function. scope chain of execution context fetched function object -- remember, activation object of "f" plus global object -- , onto copy of scope chain push current activation object. since anonymous function executing has neither arguments nor locals, no-op.

we attempt evaluate "alert"; scope chain. current activation , "f" activation not have called "alert", ask global object, , says yes, alert function. evaluate "referenced". it's not on current activation object, on f's activation object, fetch value , pass alert.

same thing on next line. global object tells there methods "alert" , "eval". of course "eval" special. eval gets copy of current scope chain string argument. eval parses string program , executes program using current scope chain. program looks "notreferenced" , finds because on current scope chain.

if have more questions area encourage read chapters 10 , 13 of ecmascript 3 specification until thoroughly understand them.

let's take deeper @ question:

when creating closure in javascript, variables in scope at time of closure's creation "enclosed" in it?

to definitively answer question need tell precisely mean "the closure" -- ecmascript 3 specification defines term. "the closure" mean the function object or the scope chain captured function object?

remember, these objects mutable -- scope chain not mutable, every object in chain mutable! whether or not there variable "at time" of creation of either scope chain or function object bit irrelevant; variables come, variables go.

for example, variables have not yet been created at time of function object's creation can captured!

function f() {  var func = function ()   {    alert(newlycreated);  }  eval("var newlycreated = 123;");  return func; } f()();  

clearly "newlycreated" not variable at time function object created because created after function object. when function object invoked, there is, on f's activation/variable object.

similarly, variable exists @ time nested function object created can deleted time function object executed.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -