3 ms·
Javascript closures are more fun than most other languages, because the scope chain can be changed after the closure has been created. Try explaining this to a
by reichstein 15y ago
Javascript closures are more fun than most other languages, because the scope chain can be changed after the closure has been created.
Try explaining this to a 6-year old:
function foo(o) {
eval("var x = 10;");
with (o) {
return function(e) {
if (typeof e == "string") eval(e);
return x;
}
}
}
this.x = 42;
var f = foo({x : 37});
alert(f("var x = 13"));
alert(f());
alert(f("delete x"));
alert(f("delete x"));
Each call returns a different variable called "x".