首页 > 代码库 > 闭包练习

闭包练习

 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

闭包练习