TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

JavaScript closures vs. anonymous functions

57 pointsby hammerhaabout 12 years ago

5 comments

tompabout 12 years ago
Wow, that was needlessly complicated and (IMO) incorrect.<p>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.<p>So, in these examples, the inner functions are closures, the outer ones are not, but <i>only</i> 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.
评论 #5393351 未加载
评论 #5393125 未加载
评论 #5394651 未加载
AlexanderDhooreabout 12 years ago
Chapter 3 in "Structure and Interpretation of Computer Programs" (SICP) explains the environment model to evaluate scheme. That's exactly what you need to understand javascript closures.<p>Only read the 3.2 part, it's not long: <a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-21.html#%_sec_3.2" rel="nofollow">http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-21.html...</a>
just2nabout 12 years ago
This question comes up so often both in IRC support channels and on SO that I don't understand why this wasn't closed as a duplicate.<p>The accepted answer also overcomplicates what could be a very simple answer.<p>In a quick Google, I found this. Asked in '08, not '12: <a href="http://stackoverflow.com/questions/111102/how-do-javascript-closures-work" rel="nofollow">http://stackoverflow.com/questions/111102/how-do-javascript-...</a>
lhnzabout 12 years ago
The second answer states it best: "They all use closure(even in the 1st example)."<p>If you're accessing a local variable from a different function scope than it was created in then you are using a closure - there is nothing special about how it was defined, this is the only distinction.
hayksaakianabout 12 years ago
What are the applications of this distinction?
评论 #5393412 未加载
评论 #5393031 未加载