闭包closure this

var name = "The Window";
var object = {
  name : "My Object",
  getNameFunc : function(){
    return function(){
      return this.name;
    };
  }
};
alert(object.getNameFunc()());

结果为

The Window

 

var name = "The Window";
var object = {
    name : "My Object",
    getNameFunc : function(){
        var that = this;
   return function(){
       return that.name;
   };
    }
};
alert(object.getNameFunc()());

结果为

My Object

 

posted @ 2021-01-26 10:48  霓裳依旧  阅读(42)  评论(0编辑  收藏  举报