代码片段一
-
var name = "The Window";
-
var object = {
-
name : "My Object",
-
getNameFunc : function(){
-
return function(){
-
return this.name;
-
};
-
}
-
};
-
alert(object.getNameFunc()());
结果:The Window
代码片段二
-
var name = "The Window";
-
var object = {
-
name : "My Object",
-
getNameFunc : function(){
-
var that = this;
-
return function(){
-
return that.name;
-
};
-
}
-
};
-
alert(object.getNameFunc()());
结果:My Object
参考:
http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures.html
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-1162676/
,如需转载,请注明出处,否则将追究法律责任。