ildar-k
9/14/2018 - 3:16 PM

Prototype inheritance

// Person constructor
function Person(firstName, lastName){
    this.firstName = firstName;
    this.lastName = lastName;
};

// Customer constructor
function Customer(firstName, lastName, phone, membership){
    Person.call(this, firstName, lastName); //call võimaldab kutsuda funktsioone teistest kontruktoritest
    this.phone = phone;
    this.membership = membership;
}

Person.prototype.greeting = function(){ // see käib vaikimis ainult person objekti külge
    return `Hello there, ${this.firstName} ${this.lastName}`;
};

// fn greeting on Person'i konstruktori oma, seega vaikimisi ei saa seda kasutada teise kontruktori põhjal tehtud objektiga.

Customer.prototype = Object.create(Person.prototype); //nii saab ühe konstruktoriga seotud prototüüpe, pookida külge teise konstruktoriga ehitatud objektide külge
Customer.prototype.constructor = Customer;

Customer.prototype.greeting = function(){ // see fn käib ainult customer objekti kohta
    return `Hello there, ${this.firstName} ${this.lastName}, welcome back`;
};

let person1 = new Person('Ignacio', 'Varga'); //
console.log(person1.greeting());

let customer1 = new Customer('Charlie', 'Harrison', 5465933, 'standard');
console.log(customer1);

console.log(customer1.greeting());