3 ms·
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 i
by elbear 13y ago
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.
- bjterry 13y agoI 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/understanding-javascript-closures/ http://javascriptweblog.wordpress.com/2010/10/25/understandi...
- elbear 13y agoI 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.