Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
Created December 9, 2010 08:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DmitrySoshnikov/734485 to your computer and use it in GitHub Desktop.
Save DmitrySoshnikov/734485 to your computer and use it in GitHub Desktop.
ECMAScript closures all environment frames
/**
* 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