Skip to content

Instantly share code, notes, and snippets.

@choonkending
Last active September 20, 2016 22:50
Show Gist options
  • Save choonkending/1b33f6805721422127c10571cb7e03d7 to your computer and use it in GitHub Desktop.
Save choonkending/1b33f6805721422127c10571cb7e03d7 to your computer and use it in GitHub Desktop.
/*
* Comparing arrow functions' lexical this binding to ordinary functions
*/
function Dog() {
// The arrow function binds this to the new Dog instance
// It is the same as writing it as
// const doSomething = function() { this.bark(); }.bind(this);
const doSomething = () => { this.bark() };
doSomething();
}
Dog.prototype.bark = function() {
console.log("woof");
}
new Dog(); // woof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment