代码如下:
<script type="text/javascript">
function make_counter(){
count = 0; function inc_count(){
count = count + 1;
return count;
}
return inc_count;
}
c1 = make_counter();
c2 = make_counter();
alert(c1());
alert(c2());
</script>困惑:
1, 为什么make_counter内部变量被保存了,按理说,函数调用完后,函数的局部变量就回收了啊?
2,上述的闭包代码,实在无法理解,C++,JAVA里面都没有这样的写法,谁能告诉我,如何从函数调用栈的角度去理解上述代码?
<script type="text/javascript">
function make_counter(){
count = 0; function inc_count(){
count = count + 1;
return count;
}
return inc_count;
}
c1 = make_counter();
c2 = make_counter();
alert(c1());
alert(c2());
</script>困惑:
1, 为什么make_counter内部变量被保存了,按理说,函数调用完后,函数的局部变量就回收了啊?
2,上述的闭包代码,实在无法理解,C++,JAVA里面都没有这样的写法,谁能告诉我,如何从函数调用栈的角度去理解上述代码?
可以参考