Created
September 29, 2015 01:11
-
-
Save jianghu52/dba55e494395ac26b6ec to your computer and use it in GitHub Desktop.
闭包例子
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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