日期:2014-05-16  浏览次数:20323 次

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