this

   var name = "The Window";
  var object = {
    name : "My Object",
    getNameFunc : function(){
      return function(){
        return this.name;
      };
    }
  };
alert(object.getNameFunc()()); //返回"The Window" ,函数体重return 的外部函数,上下文在window下.

理解this四中用法

闭包(Closure)

  var name = "The Window";
  var object = {
    name : "My Object",
    getNameFunc : function(){
      var that = this;    //注意这里
      return function(){
        return that.name;
      };
    }
  };
  alert(object.getNameFunc()()); //返回 "My Object" getNameFunc 匿名函数属于object对象的函数,this 赋给 that,导致最后return的函数依赖getNameFunc ,不会被回收。

阮一峰的网络日志-学习javascript闭包(Closure)

闭包的秘密

闭包与this学习

posted on 2017-03-30 23:39  calm01  阅读(140)  评论(0编辑  收藏  举报