this 與window.onload

 
 
var user ="THE WINDOW";
window.onload = function(){
    var user = "The window";
    var object = {
        user: "the person",
        getUserFunc: function (){
            return function(){
                return this.user; 
            };
        }
    };
    
    alert(object.getUserFunc()());  //THE WINDOW
    
};
 1 window.onload = function(){
 2     var user = "The window";
 3     var object = {
 4         user: "the person",
 5         getUserFunc: function (){
 6             return function(){
 7                 return this.user; //this指向window
 8             };
 9         }
10     };
11     
12     alert(object.getUserFunc()());  //undefined
13     
14 };

匿名函數的執行環境具備全局性。函數

相關文章
相關標籤/搜索