What is the advantage or different adding a function onto the prototype object versus simply declaring it on the constructor's variable?
this is the same, this is how we call Person
const James = new Person('James')
James.greet()
Adding greet to prototype object
const Person = name => {
this.name = name
}
Person.protoype.greet = () => console.log(`my name is ${this.name}!`)
Adding greet to constructor
const Person = name => {
this.name = name
this.greet = () => console.log(`my name is ${this.name}!`)
}
Does adding greet to constructor make duplicate of function when we call Person multiple times? I know the disavatange of object method have this problem, for example
const Person = () => ({
name: '',
greet: () => console.log(`my name is ${this.name}!`)
})
const James = Person('James') //greet copy 1
James.greet()
const Alice = Person('Alice') //greet copy 2
Alice.greet()