What happens to a JavaScript function when it is closured in as a variable? -


i've read many questions on closures , javascript on so, can't find information happens function when gets closured in. examples string literals or simple objects. see example below. when closure in function, original function preserved if change later on.

what technically happens function preserved in closure? how stored in memory? how preserved?

see following code example:

var makefunc = function () {   var fn = document.getelementbyid;  //preserving this, change later    function getoriginal() {     return fn;  //closure in   }    return getoriginal; };  var myfunc = makefunc(); var oldgetelementbyid = myfunc();  //get preserved function  document.getelementbyid = function () {  //change original function   return "foo" };  console.log(oldgetelementbyid.call(document, "mydiv"));  //calls original! console.log(document.getelementbyid("mydiv"));   //returns "foo" 

thanks comments , discussion. after recommendations, found following.

what technically happens function preserved in closure?

functions objects treated no differently other simple object closured variables (such string or object).

how stored in memory? how preserved?

to answer this, had dig through texts on programming languages. john c. mitchell's concepts in programming languages explains closured variables end in program heap.

therefore, variables defined in nesting subprograms may need lifetimes of entire program, rather time during subprogram in defined active. variable lifetime of whole program said have unlimited extent. means must heap-dynamic, rather stack-dynamic.

and more specific javascript runtimes, dmitry soshnikov describes

as implementations, storing local variables after context destroyed, stack-based implementation not fit more (because contradicts definition of stack-based structure). therefore in case closured data of parent context saved in dynamic memory allocation (in “heap”, i.e. heap-based implementations), using garbage collector (gc) , references counting. such systems less effective speed stack-based systems. however, implementations may optimize it: @ parsing stage find out, whether free variables used in function, , depending on decide — place data in stack or in “heap”.

further, dmitry shows varied implementation of parent scope in function closures:

as mentioned, optimization purpose, when function not use free variables, implementations may not save parent scope chain. however, in ecma-262-3 specification nothing said it; therefore, formally (and technical algorithm) — functions save scope chain in [[scope]] property @ creation moment.

some implementations allow access closured scope directly. example in rhino, [[scope]] property of function corresponds non-standard property __parent__.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -