首页 > Web开发 > 详细

js闭包??

时间:2018-04-28 12:41:33      阅读:129      评论:0      收藏:0      [点我收藏+]
<script>
     var name = "The Window";
     var object = {
    name : "My Object",

    getNameFunc : function(){
                            console.log("11111");
                            console.log(this);  //this == object  //调用该匿名函数的是对象
      return function(){
                              console.log("22222");
                              console.log(this); //this == window   //匿名函数下的匿名函数
      return this.name;
      };
    }

  };
  alert(object.getNameFunc()());
        
        
        //--
  var name = "The Window";

  var object = {
    name : "My Object",

    getNameFunc : function(){
      var that = this;
      return function(){
                      console.log("33333");
                       console.log(this); //this==window
                      console.log("44444");
                       console.log(that); //that==object
        return that.name;
      };

    }

  };

  alert(object.getNameFunc()());
    </script>

 

js闭包??

原文:https://www.cnblogs.com/enych/p/8966592.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!