function Person(name) {
this.name = name;
this.greeting = function() {
alert('Hi! I\'m ' + this.name + '.');
};
}
let person1 = new Person('Bob');
let person2 = new Person('Sarah');
Copy to Clipboard
"After the new objects have been created, the person1 and person2 variables contain the following objects:"
{
name: 'Bob',
greeting: function() {
alert('Hi! I\'m ' + this.name + '.');
}
}
{
name: 'Sarah',
greeting: function() {
alert('Hi! I\'m ' + this.name + '.');
}
}
Copy to Clipboard
"Note that when we are calling our constructor function, we are defining greeting() every time, which isn't ideal. To avoid this, we can define functions on the prototype instead, which we will look at later."
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_JS
MDN says defining greeting every time isn't ideal. I don't understand, we only define greeting once on the constructor function? What is not ideal about this?