Created
June 4, 2015 23:34
-
-
Save ArcticLight/c4ba97ecad82430735e1 to your computer and use it in GitHub Desktop.
Example of what Bind() does in Javascript
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 unbound = function() { | |
console.log(this.seven); | |
} | |
unbound(); //Nothing: seven isn't defined. | |
var ThisIsSeven = { | |
"seven": 7 | |
}; | |
var bound = unbound.bind(ThisIsSeven); | |
bound(); //7 is printed to the console; the "this" keyword referrs to `ThisIsSeven` | |
window.setTimeout((function() { | |
console.log("This works for anonymous functions, too,"); | |
console.log("when you have bound them like this."); | |
console.log("How many are we", this.seven); | |
}).bind(ThisIsSeven), 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment