Using arrow functions in object methods
Kayut
Posted on January 18, 2019
Is it possible to use arrow functions to define a method inside a class?
If it's possible. How can I define the method bark() in the bellow example with arrow function?
class Dog {
constructor(name, bread) {
this.name = name;
this.bread = bread;
}
bark() {
return `Bark Bark! My name is ${this.name}`;
}
}
const mini = new Dog('Mini', 'Spitz');
mini.bark();
I tried this, but it's saying: Uncaught SnytaxError.
bark = () => {
return `Bark Bark! My name is ${this.name}`;
}
💖 💪 🙅 🚩
Kayut
Posted on January 18, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.