Created
December 9, 2010 08:33
-
-
Save DmitrySoshnikov/734485 to your computer and use it in GitHub Desktop.
ECMAScript closures all environment frames
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ECMAScript closures all environment frames | |
* (by the spec), so closured "x" variable is | |
* available in the dynamic `eval`. | |
* | |
* This example is made to show the difference | |
* of the Python's closures implementation, | |
* see it here: https://gist.github.com/734482 | |
* | |
* by Dmitry A. Soshnikov | |
*/ | |
function foo(x) { | |
function bar(y) { | |
console.log(eval(k)); | |
} | |
return bar; | |
} | |
// create "bar" | |
var bar = foo(10); | |
var k = "y"; | |
// execute bar | |
bar(20); // OK, 20 - "y" | |
k = "x"; | |
bar(20); // OK, 10 - "x" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment