代码:
<script>
for(var i=0; i<3; i++){
(function(index){
alert(index);
})(i);
}
</script>
请问这个for循环中的立即执行函数,是同一个函数调用了3次?还是每次循环都新建了1个函数,相当于有3个不同函数?谢谢!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/A_re-introduction_to_JavaScript#闭包
由于没有闭包,没有循环引用所以每次都会垃圾回收这个function
仨个。
参考这里:http://segmentfault.com/q/1010000003712016/a-1020000003712251
答案应该是这个吧:每次循环都新建了1个函数,相当于有3个不同函数