Created
February 23, 2018 00:06
-
-
Save SakoMe/822f244cfdd1236e096bc12ece610936 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
// CLASSES | |
class Dog { | |
constructor() { | |
this.sound = 'woof'; | |
} | |
talk() { | |
console.log(this.sound); | |
} | |
} | |
const sniffles = new Dog(); | |
const fluffy = new Dog(); | |
$(function() { | |
// $('button.myButton').click(sniffles.talk); // => will not work | |
// $('button.myButton').click(sniffles.talk.bind(sniffles)); // => will work | |
// $('button.myButton').click(_ => sniffles.talk()); // => will work | |
}); | |
// FACTORY FUNCTION | |
const dog = () => { | |
const sound = 'woof'; | |
return { | |
talk: () => console.log(sound), | |
}; | |
}; | |
const sniffles = dog(); | |
$(function() { | |
$('button.myButton').click(sniffles.talk); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment