1. 程式人生 > >js閉包??

js閉包??

script nbsp clas fun getname div object bject span

<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閉包??