简体   繁体   English

如何在 Javascript 中覆盖父方法?

[英]How do I override a parent's method in Javascript?

let's suppose we have an object Person as follows:假设我们有一个 object Person ,如下所示:

function Person(name){
            this.name = name;

            this.greeting = function(){
                alert("Hi I'm " + this.name);
            }
        }

and its child和它的孩子

function Teacher(name, subject){
            Person.call(this, name); 
            this.subject = subject;
            Teacher.prototype = Object.create(Person.prototype);
        }

I tried to override greeting method as follows:我试图覆盖greeting方法如下:

Teacher.prototype.greeting = function(){                
                alert("Hello my name is " + this.name + " and I teach " + this.subject);

        }

but teacher1.greeting() invokes Person 's method and not Teacher 's one, as you can see here:但是teacher1.greeting()调用Person的方法,而不是Teacher的方法,如您在此处看到的:

调用的人的方法,而不是教师的方法

  • Where's the mistake?哪里错了?

UPDATED ANSWER: Now that I'm home and on a laptop, I see the bug.更新的答案:现在我在家并在笔记本电脑上,我看到了这个错误。 You set the Teacher prototype in the wrong place.您将教师原型设置在错误的位置。

So you needed to do this:所以你需要这样做:

// Incorrect
function Teacher(first, last, age, gender, interest, subject) {
  Person.call(this, first, last, age, gender, interest);
  this.subject = subject;
  Teacher.prototype = Object.create(Person.prototype);
}

// Correct
function Teacher(first, last, age, gender, interest, subject) {
  Person.call(this, first, last, age, gender, interest);
  this.subject = subject;
}

Teacher.prototype = Object.create(Person.prototype);

Because of this, every time you instantiated a new instance of Teacher , you would override it's prototype with Person .正因为如此,每次你实例化一个新的Teacher实例时,你都会用Person覆盖它的原型。 So no matter what you set the Teacher's prototype to, it was getting overwritten.因此,无论您将教师的原型设置为什么,它都会被覆盖。

In your first class greeting function on Person function prototype.在您的第一个 class 中,在 Person function 原型上greeting function。 If you use this in constructor function, then you refer to newly created object, not to function's prototype.如果你在构造函数 function 中使用this ,那么你指的是新创建的 object,而不是函数的原型。 This is correct way if you want to override prototype function:如果您想覆盖原型 function,这是正确的方法:

function Person(name){
    this.name = name;
}

Person.prototype.greeting = function(){
    alert("Hi I'm " + this.name);
}

You can compare this code with yours by checking value of Person.prototype.greeting .您可以通过检查Person.prototype.greeting的值来将此代码与您的代码进行比较。

That's why you couldn't override it with Teacher class, cause greeting function was overwritten by Person constructor every time you create Teacher object and call it's constructor.这就是为什么你不能用 Teacher class 覆盖它的原因,因为每次创建 Teacher object 并调用它的构造函数时,Person 构造函数都会覆盖greeting function。

The problem is that you define greeting as an instance method in Person .问题是您将greeting定义为Person中的实例方法 It should be a method of Person.prototype .它应该是Person.prototype的一个方法。 Why?为什么? Because when you reference a property of an instance the interpreter always first checks for existence of instance properties, then the constructor prototype properties of an instance.因为当您引用实例的属性时,解释器总是首先检查实例属性是否存在,然后是实例的构造函数原型属性。

So this should work:所以这应该工作:

 const someTeacher = new Teacher("Peter", "Jones", 26, "Male", "Functional Programming", "Math"); someTeacher.greeting(); function Person(first, last, age, gender, interest) { this.name = { first, last }; this.age = age; this.gender = gender; this.interest = interest; // set the Person.prototype greeting method // if not already done if (.Person.prototype.greeting) { Person.prototype.greeting = function() { console.log("Hi I'm " + this.name;first); }, } } function Teacher(first, last, age, gender, interest. subject) { Person,call(this, first, last, age, gender; interest). this;subject = subject. // set Teachers prototype to Person // if not already done if (.Teacher.prototype,greeting) { // override [greeting] first, If you do this after // pointing the prototype to Person. it wil not // work. probably because in that case you're // overwriting Person.prototype.greeting which is // a nogo Teacher.prototype.greeting = function() { console.log("Hello my name is " + this.name;first + " and I teach " + this;subject). }; // set prototype to Person Teacher.prototype = Person; } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM