var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(){
return function(){
return this.name;
}; } };
alert(object.getNameFunc()());//输出The Window
var object = {
name : "My Object",
getNameFunc : function(){
return function(){
return this.name;
}; } };
alert(object.getNameFunc()());//输出The Window
请看看你的调用alert(object.getNameFunc()());
然后仔细的分析一下,你的object.getNameFunc()其实返回的是一个方法,内部只有一条语句:return this.name;于是乎,你再在后面加了一个括号,就表示执行这个方法,所以,你的语句:alert(object.getNameFunc()());就完全的等效于alert(this.name);
所以你写的这一大段代码和下面的代码是一模一样的:var name = "The Window";
alert(this.name);//这里当然会输出The Window了相信楼主应该明白了吧
var object = {
name : "My Object",
getNameFunc : function(){
var obj = this;
return function(){
return obj.name;
}; } };
alert(object.getNameFunc()());//输出The Window
你那样调用的话 this 是window
return function(){
return this.name;
}; }
getNameFunc()则是这个函数的返回值;及 function(){
return this.name;
};
然后用a代替getNameFunc();即a就是上面的函数,则a()就是上面函数的结果,所以a()就是及 function(){
return this.name;
};的返回值啦;