js closure ,copy from stackoverflow
2015-08-24 11:22
633 查看
Whenever you see the function keyword within another function, the inner function has access to variables in the outer function.
That is a closure. A function doesn't have to return in order to be called a closure. Simply accessing variables outside of your immediate lexical scope creates a closure.
However, since
The simplest example of a closure is this:
created. Together with the function arguments and the parent object,
this execution context also receives all the variables declared outside
of it (in the above example, both 'a' and 'b').
It is possible to create more than one closure function, either by
returning a list of them or by setting them to global variables. All of
these will refer to the same
Here the number
On the other hand, JavaScript always uses references when dealing with objects. If say, you called
function foo(x) { var tmp = 3; function bar(y) { alert(x + y + (++tmp)); // will alert 16 } bar(10); } foo(2);This will always alert 16, because
barcan access the
xwhich was defined as an argument to
foo, and it can also access
tmpfrom
foo.
That is a closure. A function doesn't have to return in order to be called a closure. Simply accessing variables outside of your immediate lexical scope creates a closure.
function foo(x) { var tmp = 3; return function (y) { alert(x + y + (++tmp)); // will also alert 16 } } var bar = foo(2); // bar is now a closure. bar(10);The above function will also alert 16, because
barcan still refer to
xand
tmp, even though it is no longer directly inside the scope.
However, since
tmpis still hanging around inside
bar's closure, it is also being incremented. It will be incremented each time you call
bar.
The simplest example of a closure is this:
var a = 10; function test() { console.log(a); // will output 10 console.log(b); // will output 6 } var b = 6; test();When a JavaScript function is invoked, a new execution context is
created. Together with the function arguments and the parent object,
this execution context also receives all the variables declared outside
of it (in the above example, both 'a' and 'b').
It is possible to create more than one closure function, either by
returning a list of them or by setting them to global variables. All of
these will refer to the same
xand the same
tmp, they don't make their own copies.
Here the number
xis a literal number. As with other literals in JavaScript, when
foois called, the number
xis copied into
fooas its argument
x.
On the other hand, JavaScript always uses references when dealing with objects. If say, you called
foowith an object, the closure it returns will reference that original object!
function foo(x) { var tmp = 3; return function (y) { alert(x + y + tmp); x.memb = x.memb ? x.memb + 1 : 1; alert(x.memb); } } var age = new Number(2); var bar = foo(age); // bar is now a closure referencing age. bar(10);As expected, each call to
bar(10)will increment
x.memb. What might not be expected, is that
xis simply referring to the same object as the
agevariable! After a couple of calls to
bar,
age.membwill be 2! This referencing is the basis for memory leaks with HTML objects.
相关文章推荐
- javascript——js string 转 int 注意的问题——parseInt
- JsonArray 遍历
- js获得文件除了拓展名以外的其他部分
- js获得当前系统时间
- JavaScript实现新闻上下滚动
- 如何判断js中的数据类型:typeof、instanceof、 constructor、 prototype方法比较
- 纯javascript判断查询日期是否为有效日期
- js-事件捕获与事件冒泡原理 IE和DOM之间存在哪些主要差别
- js思路总结
- js实现类似MSN提示的页面效果代码分享
- 百度地图商家标注,查询附近3000米内的商家并标到地图上
- [转]精通JS正则表达式
- js 二维数组定义
- [LeetCode][JavaScript]Missing Number
- javascript--闭包
- 关于瀑布流
- javascript中的this应用
- 原生js解决跨浏览器兼容问题
- javascript实现支持移动设备画廊
- iscroll.js的使用