Skip to content

Instantly share code, notes, and snippets.

@SakoMe
Created February 23, 2018 00:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SakoMe/822f244cfdd1236e096bc12ece610936 to your computer and use it in GitHub Desktop.
Save SakoMe/822f244cfdd1236e096bc12ece610936 to your computer and use it in GitHub Desktop.
// 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