如何访问被覆盖的方法

how to access overwritten method

我有以下内容:

// 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`;
}

所以对于 customer1,我可以使用 console.log(customer1.greeting()) 访问 customer.greeting()。如何访问 customer1 的 Person.greeting()?

更新:

Felix Kling 提供的答案很有趣,但似乎没有回答问题。他似乎在展示如何覆盖被覆盖的方法。我的问题是是否可以访问这两种问候语方法?如果可以,如何访问?

如果我们使用 class 我认为以下应该有效:

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

这是否正确,是否可以在不使用 classes 的情况下做同样的事情?

第二次更新

在 es5 中我们可以做到:

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

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

获取问候语的父方法。我们可以使用:

customer1.greeting() 

获取客户​​问候方式。

在 es6 中我们可以使用 classes 并执行以下操作:

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());

所以在 class 中我们可以使用 super 访问 Person.greeting 但以下不起作用

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

所以,给定 john,谁是客户,客户是人,有没有办法在 class 之外访问 super?

Person.prototypeCustomer.prototype的原型。所以你可以这样调用 Person.prototype.gretting

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

类 让这更容易顺便说一句:

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