Javascript从带有参数的构造函数调用prototype方法

时间:2017-06-09 15:04:17

标签: javascript oop methods constructor

我试图从教授构造函数中调用Person.showInfo(),其名称和年龄作为参数。可能吗?。请参阅下面的评论,谢谢。

var Person = function(_name, _age) {
    this.name = _name;
    this.age = _age;
}

Person.prototype.showInfo = function() {
    console.log(this.name, this.age);
};

var Professor = function(_name, _age, _course) {
    Person.call(this, _name, _age);
    this.course = _course;
};

Professor.prototype = Object.create(Person.prototype);
Professor.prototype.constructor = Professor;

Professor.prototype.showInfo = function() {
    // How to call Person.showInfo() here ???
    // Person.prototype.showInfo() ...showing the Professor's name and age
    console.log(this.course);
};

1 个答案:

答案 0 :(得分:0)

Person.prototype.showInfo.call(this);

只需按原样调用该函数,但更改上下文。