Skip to main content
14 events
when toggle format what by license comment
Feb 3, 2012 at 4:57 vote accept Matt Lo
Jan 23, 2012 at 21:02 comment added user1106925 @nnnnnn: I see what you mean. Yes, you're right. You don't have closures without first-class functions that can be exported while retaining their enclosing environment, and the existence of a closure itself isn't particularly useful until you take particular advantage of it. Sort of like car insurance. :)
Jan 23, 2012 at 20:51 comment added nnnnnn @amnotiam - I didn't mean that it wasn't a closure either way. I just meant that unless you export the inner function in some way the closure concept is not particularly relevant. Plenty of languages that don't have closures allow nested functions, so an explanation of closures should mention what extra benefits they provide beyond simple nesting.
Jan 23, 2012 at 15:52 comment added user1106925 @nnnnnn: I disagree that the inner function has to be executed later for it to be a closure. A function is a lexical environment that has a reference to its enclosing lexical environment, and therefore its variables. This is a closure. We often use the inner function after the outer has returned, and can enjoy the benefit of the closure at that time, but the closure exists either way. It's just short-lived if we don't export the inner function.
Jan 23, 2012 at 5:19 comment added nnnnnn @amnotiam - Even languages that don't have closures support what you've described: that is just "normal" nested function scope. Where the closure concept comes in is when the inner function is executed after the outer function has returned, something that isn't even possible in some languages. (I'm confident you know that, but you didn't say it.)
Jan 23, 2012 at 5:14 comment added nnnnnn @MattLo - if you want a hardline approach, comparing closures to anonymous functions is comparing apples to oranges. The terms are not equivalent, but some people use them interchangeably because they don't know the difference.
Jan 23, 2012 at 5:02 answer added nnnnnn timeline score: 4
Jan 23, 2012 at 4:59 vote accept Matt Lo
Jan 23, 2012 at 5:03
Jan 23, 2012 at 4:58 comment added user1106925 Imagine two functions. Doesn't matter if they're anonymous or not, but one function is inside the other. Now lets say that the outer function has a variable foo. Even though foo is inside the outer function, but not the inner function, the inner function can still access foo. This is because the inner func creates a "closure" over the variable environment of the outer. So a "closure" is this phenomenon of nested variable environments being able to access the variables of the environments that contain it.
Jan 23, 2012 at 4:47 comment added Matt Lo well said, as in your other posts too. I'm looking for a hardline approach though (like some of your earlier responses) but more on definition of closures in contrast to how the term is used in JavaScript and with the term "anonymous functions." Is it safe to say both terms are interchangeable with each other in JavaScript?
Jan 23, 2012 at 4:37 comment added user1106925 With respect to closures, a function is a function in JavaScript. Doesn't matter if it's anonymous.
Jan 23, 2012 at 4:36 answer added wizzard0 timeline score: 1
Jan 23, 2012 at 4:35 answer added Borealid timeline score: 1
Jan 23, 2012 at 4:33 history asked Matt Lo CC BY-SA 3.0