闭包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
原文地址:https://www.cnblogs.com/yangai/p/14329099.html