Ok so I'm learning some topics and I'm learning prototypes. I see that this works:
function Person(name) {
this.name = name || 'Luis Felipe Zaguini';
this.sayHi = function() {
console.log(`Hey there, ${this.name}!`);
}
this.sayGoodBye = function() {
console.log(`Good bye, ${this.name}!`);
}
}
let me = new Person();
me.sayHi();
let other = new Person('Other');
other.sayGoodBye();
But that also works:
function Person(name) {
this.name = name || 'Luis Felipe Zaguini';
this.sayHi = function() {
console.log(`Hey there, ${this.name}!`);
}
}
Person.prototype.sayGoodBye = function() {
console.log(`Good bye, ${this.name}!`);
}
let me = new Person();
me.sayHi();
let other = new Person('Other');
other.sayGoodBye();
They end up 'in the same way'. Which version is better? Are there some disadvantages? It seems to me that the first way (defining methods inside the function object) looks clearer. What do you people think?