DCR
DCR

Reputation: 15665

how to access overwritten method

I have the following:

// 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');

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

const customer = new Customer('Tom', 'Smith', '555-555-5555', 'Standard');

// Inherit the Person prototype methods
Customer.prototype = Object.create(Person.prototype);

// Make customer.prototype return Customer()
Customer.prototype.constructor = Customer;

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

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

so for customer1 I can access the customer.greeting() with console.log(customer1.greeting()). How do I access the Person.greeting() for customer1?

UPDATE:

the answer provided by Felix Kling is interesting but it doesn't seem to answer the question. He seems to be showing how to overwrite the overwritten method. My question is it possible to access both greeting methods and if so how?

If we used class the following I think should work:

class Customer extends Person {
    greeting(); //should return Customer.greeting()
    super.greeting(); //should return Person.greeting()  
}

Is this correct and is it possible to do the same without using classes?

SECOND UPDATE

in es5 we can do:

console.log(customer1.constructor.prototype.greeting.call(customer1))

or

console.log(Object.getPrototypeOf(customer1.constructor.prototype).greeting.call(customer1)); 

to get the parent method of greeting. and we can use:

customer1.greeting() 

to get the customer greeting method.

in es6 we can use classes and do the following:

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

    greeting() {
       return `Hello there ${this.firstName} ${this.lastName}`;
    }
}

class Customer extends Person {
    constructor(firstName, lastName, phone, membership) {
       super(firstName, lastName);
       this.phone = phone;
       this.membership = membership;
     }

     greeting(){
         console.log(super.greeting()) 
         return `Hello there ${this.firstName} ${this.lastName} ${this.phone}`
    }

}

const john = new Customer('John', 'Doe', '555-555-5555', 'Standard');
console.log(john.greeting());

so within the class we can access the Person.greeting using super but the following does not work

console.log(john.super.greeting());

so, given john, who is a customer and customers are persons is there a way to access super outside of the class?

Upvotes: 0

Views: 59

Answers (1)

Felix Kling
Felix Kling

Reputation: 816354

Person.prototype is the prototype of Customer.prototype. So you can call Person.prototype.gretting like so:

Customer.prototype.greeting = function() {
  Object.getPrototypeOf(this.constructor.prototype).greeting.call(this);
}

Classes make this much easier btw:

class Customer extends Person {
  greeting() {
    super.greeting();
  }
}

Upvotes: 3

Related Questions