TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

JavaScript closures vs. anonymous functions

57 点作者 hammerha大约 12 年前

5 条评论

tomp大约 12 年前
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 未加载
AlexanderDhoore大约 12 年前
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>
just2n大约 12 年前
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>
lhnz大约 12 年前
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.
hayksaakian大约 12 年前
What are the applications of this distinction?
评论 #5393412 未加载
评论 #5393031 未加载