I've noticed that a lot of people (though not the author of this article) confuse the terms "lambda expression" and "closure". Perhaps this confusion is worth taking a moment to address here.<p>A closure, as explained in the article, is a pair of a code pointer and an environment. It is an object "inside the machine" that exists at runtime.<p>A lambda expression is a syntactic entity, a piece of source code: something you can see on the screen.<p>A lambda expression <i>evaluates to</i> a closure, just as an integer literal (a string of digits) evaluates to an integer, or a 'new' expression evaluates to an instance (object).
Can't talk about closures without mentioning this excellent piece <a href="http://mrale.ph/blog/2012/09/23/grokking-v8-closures-for-fun.html" rel="nofollow">http://mrale.ph/blog/2012/09/23/grokking-v8-closures-for-fun...</a>
Lua uses a concept called "upvalues" to refer to variables from enclosing scopes. The main observation here seems to be that you can statically determine a unique index for all local variables in any enclosing scope. I'm not sure exactly how the details work in cases where there are sibling scopes, but anyway it seems like a useful concept and i am surprised the concept/terminology is not more common.
Every implementation of a closure I've encountered creates a new Capture class which stores references/copies of all variables the closure uses. I think they do this even if the closure doesn't reference any local variables - only class-level ones. In this case, wouldn't it be better to just implement it as a local function of the parent class instead of creating a new class? Or does it not matter because the cost of calling this.foo is negligibly similar to this.parent.foo?
AFAICT Closures are still much faster than Function.prototype.bind.<p>(<a href="http://jsperf.com/bind-vs-closure-setup/44" rel="nofollow">http://jsperf.com/bind-vs-closure-setup/44</a>)<p>Could anyone who knows more about the implementation details point to an explanation of what's going on under the surface?