var name = "The Window";
var object = {
name : "My Object",
getName: function(){
return this.name;
}
};
(object.getName = object.getName)(); // The Window
我的理解是:
this取决于定义环境而不取决于执行环境。
在这里
(object.getName = object.getName)();
相当于
(object.getName = function () {
return this.name;
})();
// 拆分开来看
function () {
return this.name;
}
// 这里的this = window
// 然后window.name在前面已经定义过了 var name = "The Window"; 定义全局变量
// 所以实际上执行的是
(function () {
return this.name;
})(); // The Window
网友评论