javascript闭包[两个小测试例子]

一.程序一

<script>
var name = "The Window";
var Object_a = {
  name : "My Object",
  getNameFunc : function(){
               var that = this;
    return function(){
      return that.name;
    };
  }
};
alert(Object_a.getNameFunc()());
</script>

 

结果:My Object

 

二.程序二

<script>
var name = "The Window";
var Object_a = {
  name : "My Object",
  getNameFunc : function(){
    return function(){
      return this.name;
    };
  }
};
alert(Object_a.getNameFunc()());
</script>

 

结果:The Window

你可能感兴趣的:(JavaScript,JavaScript)