【发布时间】:2013-11-03 07:13:55
【问题描述】:
我想通过应用经典继承在扩展的 javascript“类”中调用超级方法。
function Person(name, age) {
this._name = name;
this._age = age;
}
Person.prototype.exposeInfo = function() {
alert(this._name + ' - ' + this._age);
}
function Employee(name, age) {
this.parent.constructor.call(this, name, age);
}
Employee.prototype.exposeInfo = function() {
alert('Call employee');
this.parent.exposeInfo();
}
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;
Employee.prototype.parent = Person.prototype;
var p1 = new Person('John Doe', 30);
p1.exposeInfo();
var p2 = new Employee('John Foobar (empl.)', 35);
p2.exposeInfo();
问题是该方法没有在扩展类中被调用,而只是在父类(Person)中被调用。
【问题讨论】:
-
你打错了
this。
标签: javascript inheritance super