Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
Last active November 5, 2017 02:20
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/e7e57fe12b47b67b89227b9326f1bfbf to your computer and use it in GitHub Desktop.
Save DmitrySoshnikov/e7e57fe12b47b67b89227b9326f1bfbf to your computer and use it in GitHub Desktop.
/**
* JavaScript runtime-augmented scope example.
*/
let x = 10;
let o = {x: 30};
let storage = {};
(function foo(flag) {
if (flag == 2) {
eval("var x = 20;");
}
if (flag == 3) {
storage = o;
}
with (storage) {
// "x" may be resolved either
// in the global scope - 10, or
// in the local scope of a function - 20
// (created via "eval" function), or even
// in the "storage" object - 30
console.log(x); // ? - scope of "x" is undetermined at compile time
}
// organize recursion on 3 calls
if (flag < 3) {
foo(++flag);
}
})(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment