var name = "小明",
person = {
name : "小红",
getName : function(){
return function(){
return this.name;
};
}
};
console.log(person.getName()()); // 小明
为什么最后打印出来的“小明”而不是“小红”?
看不太懂person.getName()()这句后面两个括号的意思。
//新手勿喷,谢谢
person.getName()
结果为一个函数:person.getName()()
即执行这个返回的函数,函数内容是return this.name
。此时,作用域为全局,所以this.name
等同于window.name
也就是“小名”