News for hackers. Stuff that matters.
Re:How do JavaScript closures work? by reichstein on Friday June 24, 2011 @02:08PM (#2693204)
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".

There are no comments on this comment yet.