闭包是怎么形成的
2013-09-24 10:28
344 查看
犀牛书第8章第8节,详细说明了闭包的形成过程。
a function is executed in the scope in which it was defined.
When a function is invoked, a call object is created for it and placed on the scope chain. When the function exits, the call object is removed from the scope chain. When no nested functions are involved, the scope chain is the only reference to the call object. When the object is removed from the chain, there are no more references to it, and it ends up being garbage collected.
But nested functions change the picture. If a nested function is created, the definition of that function refers to the call objects because that call object is the top of the scope chain in which the function was defined. If the nested function is used only within the outer function, however, the only reference to the nested function is in the call object. When the outer function returns, the nested function refers to the call object, and the call object refers to nested function, but there are no other references to either one, and so both objects become available for garbage collection.
Things are different if you save a reference to the nested function in the global scope. You do so by using the nested function as the return value of the outer function or by storing the nested function as the property of some other object. In this case, there is an external reference to the nested function, and the nested function retains its reference to the call object of the outer function. The upshot is that the call object for that one particular invocation of the outer function continues to live, and the names and values of the function arguments and local variables persist in this object. JavaScript code cannot directly access the call object in any way, but the properties it defines are part of the scope chain for any invocation of the nested function.
-------------------------简单总结--------------------------
当嵌套函数被返回,被外部的变量引用,或者作为外部对象的属性时,形成闭包。
形成闭包之后,原本应该已经被垃圾回收的变量,都能继续使用。
a function is executed in the scope in which it was defined.
When a function is invoked, a call object is created for it and placed on the scope chain. When the function exits, the call object is removed from the scope chain. When no nested functions are involved, the scope chain is the only reference to the call object. When the object is removed from the chain, there are no more references to it, and it ends up being garbage collected.
But nested functions change the picture. If a nested function is created, the definition of that function refers to the call objects because that call object is the top of the scope chain in which the function was defined. If the nested function is used only within the outer function, however, the only reference to the nested function is in the call object. When the outer function returns, the nested function refers to the call object, and the call object refers to nested function, but there are no other references to either one, and so both objects become available for garbage collection.
Things are different if you save a reference to the nested function in the global scope. You do so by using the nested function as the return value of the outer function or by storing the nested function as the property of some other object. In this case, there is an external reference to the nested function, and the nested function retains its reference to the call object of the outer function. The upshot is that the call object for that one particular invocation of the outer function continues to live, and the names and values of the function arguments and local variables persist in this object. JavaScript code cannot directly access the call object in any way, but the properties it defines are part of the scope chain for any invocation of the nested function.
-------------------------简单总结--------------------------
当嵌套函数被返回,被外部的变量引用,或者作为外部对象的属性时,形成闭包。
形成闭包之后,原本应该已经被垃圾回收的变量,都能继续使用。
相关文章推荐
- 函数执行时的作用域链和活动对象是怎么形成的及与闭包的关系
- 专家是怎么形成的
- 规范是怎么形成的?
- 最近看到搜有的门户网站出现官网字样,这种情况是怎么形成的?希望高手帮助释疑!
- JavaScript函数调用时的作用域链和调用对象是如何形成的及与闭包的关系
- javascript的闭包的形成
- 原来是这样:C#中的闭包是怎么捕获变量的
- 我简单写写今天中文编码的混乱局面是怎么形成的
- 深入贯彻闭包思想,全面理解JS闭包形成过程
- 如果exclipe中Java视图中Consol跑偏了单独形成一个弹框怎么办?
- JavaScript函数调用时的作用域链和调用对象是如何形成的及与闭包的关系
- Java 8怎么了:局部套用vs闭包
- 怎么计算一个集合的幂集(所有子集形成的集合)的长度?涉及组合数学基础
- Java 8怎么了:局部套用vs闭包
- 集线器级联增加了网络数量,效率降低了,形成了大的冲突域,怎么解决?
- javascript的闭包的形成
- 【js基础】怎么理解javascript中的闭包?
- 怎么优化源代码,源代码的形成是什么?
- 循环中的闭包,怎么返回当前索引
- 相片上的噪点是怎么形成的?