js闭包??

<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>

猜你喜欢

转载自www.cnblogs.com/enych/p/8966592.html