Skip to content

Instantly share code, notes, and snippets.

@jianghu52
Created September 29, 2015 01:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianghu52/dba55e494395ac26b6ec to your computer and use it in GitHub Desktop.
Save jianghu52/dba55e494395ac26b6ec to your computer and use it in GitHub Desktop.
闭包例子
var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(){
return function(){
return this.name;
};
}
};
alert(object.getNameFunc()()); //out put The window
var object2 = {
name : "My Object",
getNameFunc : function(){
var that = this;
return function(){
return that.name;
};
}
};
alert(object2.getNameFunc()()); //My Object
var object3 = {
name : "My Object",
getNameFunc : function(){
return function(){
return name;
};
}
};
alert(object3.getNameFunc()()); //out put The window
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment