Last active
September 20, 2016 22:50
-
-
Save choonkending/1b33f6805721422127c10571cb7e03d7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* 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