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 };
匿名函数的执行环境具有全局性。