In ES6 (ECMAScript 2015) you can define a class method either in the constructor or the body, what's the difference?
class Person {
constructor(name) {
this.name = name;
this.sayName = () => {
console.log('hello i am', this.name);
}
}
}
class Person2 {
constructor(name) {
this.name = name;
}
sayName() {
console.log('hello i am', this.name);
}
}
My guess is, in Person
, the method sayName
is being recreated each time we instantiate a Person
object, and in Person2
, we are applying the method to the prototype? (just a guess)