20170714_js闭包变量重置
标签:cti length normal pac console white var ++ for
html>
body>
script>
function createFunctions(){
var result = new Array();
for (var i=0; i 10; i++){
result[i] = function(){
return i;
};
}
return result;
}
var funcs = createFunctions();
for (var i=0; i funcs.length; i++){
console.log(funcs[i]());
}
funcs = ‘‘;
var funcs = createFunctions();
for (var i=0; i funcs.length; i++){
console.log(funcs[i]());
}
/script>
body>
html>
执行到funcs=‘‘之前,匿名函数里的i都是10,执行funcs=‘‘后,匿名函数的引用被删除,不再被引用内存就会被回收,那么匿名函数里的i就被初始化了
20170714_js闭包变量重置
标签:cti length normal pac console white var ++ for
原文地址:http://www.cnblogs.com/tiantian123/p/7169479.html
评论