How JavaScript closures work under the hood
dmitryfrank.com
dmitryfrank.com
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.
A lambda expression is a syntactic entity, a piece of source code: something you can see on the screen.
A lambda expression evaluates to a closure, just as an integer literal (a string of digits) evaluates to an integer, or a 'new' expression evaluates to an instance (object).
A lambda procedure is just an anonymous procedure.
A lambda is not necessary a closure and a closure is not necessary a lambda.
Some programming classes takes some time to describe this habit of unnecessary wrapping[2]. From
```
if bool_var then true else false
```
, to theses functions-written-as-classes, or even, depending on the language you use, function that wraps other functions disguised as operators ```
map(function add(x, y) { return x + y }, list)
// aka `map + list` in deeply functional languages
```
[1] Java had many single method interfaces that can now be replaced by lambdas[2] see Dan Grossman proglang on Coursera (taken from his original classes at UoWashington)
+
function add(x, y) { return x + y; }
in general in strict, effectful languages. The transformation is called "eta expansion" (technically, I performed two eta expansions and an un-currying) and we really would like it to be valid—it implies conceptual freedom to introduce and eliminate abstractions, but it rarely is. The issue being that it introduces thunks where computation or effects can hide.(http://jsperf.com/bind-vs-closure-setup/44)
Could anyone who knows more about the implementation details point to an explanation of what's going on under the surface?
Let me know if you have any questions about it
https://github.com/nodejs/node/blob/9fddd83cf9adf505bce2e237...
https://github.com/nodejs/node/blob/9fddd83cf9adf505bce2e237...