zakkudesu
10/2/2019 - 1:24 AM

Prototypical Inheritance

//Prototypical Inheritance 

//person constructor

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

//Greeting
Person.prototype.greeting = function(){
  return `Hello there ${this.firstName} ${this.lastName}`;
}

const person1 = new Person('John', 'Doe');

console.log(person1.greeting());

//Customer constructor
function Customer(firstName, lastName, phone, membership){
  Person.call(this, firstName, lastName);

  this.phone = phone;
  this.membership = membership;
}

//Inherit the Person prototype methods
Customer.prototype = Object.create(Person.prototype);
//Make customer.protype return Customer()
Customer.prototype.constructor = Customer;

//Create customer 
const customer1 = new Customer('Tom', 'Smith', '555-555-5555', 'standard');

console.log(customer1);

//Customer greeting
Customer.prototype.greeting = function(){
  return `Hello there ${this.firstName} ${this.lastName} welcome back.`;
}

console.log(customer1.greeting());