Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* 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