4 05 JavaScript Aside Prototypal Inheritance and Function Constructors

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

One question, I dont fully understand the use of the prototype, because if I include the greet() function in the Person () constructor function as a method, all the new objects created with that function will have acces to greet() too.
Example:
function Person (firstName, lastName){

this.firstName = firstName;
this.lastName = lastName;
this.greet = function (){
console.log('Hello, ' + this.firstName + ' ' + this.lastName);
}
}

let john = new Person ('John', 'Doe');
let jane = new Person ('Jane', 'Doe');
console.log(john);
console.log(jane);
john.greet();
jane.greet();

Outputs:
Person {
firstName: 'John',
lastName: 'Doe',
greet: [Function (anonymous)]
}
Person {
firstName: 'Jane',
lastName: 'Doe',
greet: [Function (anonymous)]
}
Hello, John Doe
Hello, Jane Doe


PS: Thanks a lot for your courses, I love the way you go deep into de concepts!!!

rodrigomarsan
visit shbcf.ru