闭包与回调有何不同?
我问有关回电的问题并得到另一个问题(见评论)。
如何是一个闭包不同于回调?
I asked a question about callbacks and arrived at another question (see comment). How is a closure different from a callback?
检查这里的介绍: http://jibbering.com/faq/faq_notes/closures.html 。它可以帮助您更好地了解闭包与函数的关系。
Check the introduction in this: http://jibbering.com/faq/faq_notes/closures.html. It can help you understand better how closures relate to functions.
这里是一组闭包示例: http://www.javascriptkit.com/javatutors/closures2.shtml
Here is a set of closure examples: http://www.javascriptkit.com/javatutors/closures2.shtml
基本上,回调就像一个函数指针。使它成为闭包的位是该函数访问它所在的上下文上的任何东西,就像它外面的变量一样。当这种情况发生时,函数将使用变量的当前值(而不是复制它们)。参见示例4.
Basically, the callback is like a function pointer. The bit that makes it a closure, is when that function accesses anything on the context where it lives, like variables outside it. When that happens, the function will use the current values of the variables (as opposed to copy them). See example 4.