有以下两段代码,涉及的是js闭包的问题,我写了下,得出了答案,但不知道对不对,希望大家帮忙看下
代码段1:
var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(){
return function(){
return this.name;
};
}
};
alert(object.getNameFunc()());
我认为这个结果应该是The Window
代码段2:
var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(){
var that = this;
return function(){
return that.name;
};
}
};
alert(object.getNameFunc()());
我认为这个结果应该是My Object,
请大家看看我的答案对不对