闭包练习

 var name = "window"; 2 var foo = { 3             name: "hello", 4             setName:function(){ 5                 return function(){ 6                     return this.name; 7                 }; 8             } 9         }; 10         alert(foo.setName()());  11 // window

复制代码
复制代码
 1 var name = "window";
 2         var foo = {
 3             name: "hello",
 4             setName:function(){
 5                 var that = this;
 6                 return function(){
 7                     return that.name;
 8                 };
 9             }
10         };
11         alert(foo.setName()());
12 //hello
posted @ 2016-11-07 22:12  小太阳8  阅读(100)  评论(0编辑  收藏  举报