« Zi Ying | 首页 | 沙克 »

陈锐达

留言(1 条)

学习Javascript闭包(Closure) 留言:

尝试解答代码段一:
getNameFunc: function() {//假设函数名为A
return function()/*假设函数名为B*/ { return this.name; };
}
在函数里面构建函数的时候,闭包产生。
在函数B内调用函数A的this.name,由于函数A没有name属性,所以就去找全局变量name,找到了,所以返回“The Window”,要是没有找到,则返回“undefined”。

代码段二可以尝试将代码更改为:
var _this = this;
return function() { return _this.name +"__"+ this.name; };