Skip to content

Instantly share code, notes, and snippets.

@ArcticLight
Created June 4, 2015 23:34
Show Gist options
  • Save ArcticLight/c4ba97ecad82430735e1 to your computer and use it in GitHub Desktop.
Save ArcticLight/c4ba97ecad82430735e1 to your computer and use it in GitHub Desktop.
Example of what Bind() does in Javascript
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