4 ms·
Wow, that was needlessly complicated and (IMO) incorrect. Any function that has free variables that are not global variables is a closure. Another way to think
by tomp 14y ago
Wow, that was needlessly complicated and (IMO) incorrect.
Any function that has free variables that are not global variables is a closure. Another way to think about it: any function that cannot be implemented in C is a closure.
So, in these examples, the inner functions are closures, the outer ones are not, but only because the outer ones are used in a global scope. If you enclosed everything in another function, then also the outer functions would be closures.
- bdfh42 14y agoCorrect - no idea why people get confused. Closures are when you want "This this in that" and vanilla anonymous functions when you want "That this in that" ;)
- pygy_ 14y ago> free variables that are not global variables These are named "upvalues" in Lua parlance (even though they are read/write, the "value" part is a leftover of a previous Lua version where these variables were read-only and frozen from the closure viewpoint). It really simplifies the discourse... Are there other short names for them in other languages? I don't know of any. > So, in these examples, the inner functions are closures, the outer ones are not, but only because the outer ones are used in a global scope. If you enclosed everything in another function, then also the outer functions would be closures. This is probably nitpicking, but in the examples on Stack Overflow, the outer functions don't close over locals of the parent scope. Their closure is thus empty, and I don't call these functions closures. Not sure if it is correct, though.
- tomp 14y agoHm... let me be more explicit: for(var i = 0; i < 10; i++) { setTimeout(function f() { console.log(i); }, 1000); } Here, technically speaking, `f` is not a closure, since it doesn't close over any local variables, since there are none (`i` is global). function a() { for(var i = 0; i < 10; i++) { setTimeout(function f() { console.log(i); }, 1000); } } Now, `f` is a closure, since it closes over the variable `i`, which is local to `a`. Note, however, that `f` closes over the variable, not over its value. That is the reason that I prefer to call such variables (heap-allocated) boxes, not upvalues.
- mnarayan01 14y agoYou appear to be using a pretty non-standard definition of "closure". Generally speaking (AFAIK), a closure is the function and its referencing environment (with the env potentially simplified to only those variables which occur free in the function). In both your examples `i` occurs free, so it would be part of the closure. Since `i` is global(ish) I guess an interpreter could potentially skip placing it in the closure as an optimization, but I'm not seeing any value to not considering it as part of the closure from a language-understanding standpoint. As an aside...both of your examples have the same effect: logging "10" 10 times. Did you mean something like for(var i = 0; i < 10; i++) { (function (_i) { setTimeout(function f() { console.log(_i); }, 1000); })(i); } instead?
- tomp 14y agoI decided to exclude global variables from my definition of closure to make it perfectly clear that closure, as it exists in JavaScript, LISP, Python and most other functional/modern languages, is different from raw function that can be defined in C, Java, and similar. No, I deliberately chose the "wrong" example, and included a Note, saying that the function doesn't close over the values of its free variables, but rather over the whole variables (in different words, over the references to the values).
- bitwize 14y agoThe historical Lisp term is "upwards funargs".
- coldtea 14y agoYour response might be technically correct, but it's not really what the reader was asking or what is interesting to ask. Not to mention that the winning answer addresses that in the very top of his reply.