Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I can get anything I need to done in JS and I have no idea what a closure is. I've even written an emulator in it.

Took me a little while reading to figure out what it is. Apparently it's just using vatiables in a lambda that are from a scope higher than it. I do that all the time, I just didn't know it had a special name. To be fair, I didn't know JS could do lambdas. I've only used them in C# and possibly Python.

Well, I guess it's like a closure when you use functions as methods in objects in JS, since they can be defined with this.MyMethod = function(){...} which can then access the object's stuff.



C# people tend to use lambda to mean lambdas, closures and sometimes anonymous functions.

Javascript people use closure to mean lambdas, closures and sometimes anonymous functions.

Ask two programmers the difference between lambdas, closures and anonymous functions and be prepared for a different and probably wrong explanation from most of them.

Including me.

And that's the only reason you don't know what a closure is, if you hung out around javascript programmers a bit more in RL or online you'd have heard it constantly.

If it was described to you, you'd know what it is.


Yeah, it's language sugar to capture variables (including automatics) from an outer scope and make them available to an anonymous function. This may involve copying the variables to other storage (e.g. to heap) if the anonymous function will be run outside the context of its caller.

Also, the language may, if it provides some sugar to allow the anonymous function to appear to return to the caller, e.g. through async/yield/co-routine type semantics, also transparently copy back the variables to the callers' locations.

As per another commentator, 'lambdas', 'closures' and 'anonymous functions' are used equivalently by different people and/or fields.

As a C programmer primarily, I was long confused why higher-level programmers went on about "closures". They're nothing special, we do this kind of stuff in C all the time. We have to do it by hand ourselves, the language doesn't give us shortcuts. It's not really mysterious and we don't have fancy names for it. Sometimes I wonder if the reason some higher-level programmers treat these things so reverentially is, perhaps, because there is an element of mysticism to it, borne of lack of understanding of what is happening "under the hood"?


Which is why when I want to know that you actually understand something as basic as closures in Javascript, I don't ask for the definition. I ask you to demonstrate it.


But if you don't know what it's called, or what the term means, then you're not going to be able to do that either.


I don't think everyone agrees, but I think usually, a closure refers to the implementation of that concept, and not the concept itself. The implementation being a pair of the static code alongside the captured environment.

Also, I think most would agree that if the "higher scope" is the global scope, then it isn't actually a closure. For example, C functions can access globals, but C has no closures (barring various extensions).


You've never passed a callback to a JS function?


That's an anonymous function not a closure. A closure references non-local environments and can "access those non-local variables even when invoked outside of its immediate lexical scope" as Wikipedia puts.


I don't understand your contradiction to the parent poster. All JavaScript functions are closures, including anonymous functions that don't reference any lexically-scoped variables, and even named functions at the top level, which close over the global environment[1]. Also, callback functions don't have to be anonymous functions, you can just as easily pass named functions.

1: http://javascriptweblog.wordpress.com/2010/10/25/understandi...


I know that confusing anonymous functions with closures is a common misconception. I also know that callback functions don't need to be anonymous, but a lot of the times they are, at least in the JavaScript code that I've seen.

Thank you for the clarification that all JavaScript functions are closures though.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: