用function 分别定义Person和Student类模型,其中Student从Person继承,并重写toString()方法
// 定义Person构造器
function Person(name) {
this.name = name;
}
// 在Person.property中添加toString方法
Person.prototype.toString = function() {
document.write(this.name);
}
// 定义Student构造器
function Student(name, course) {
// 从Person继承
this.newObj = Person;
this.newObj(name);
delete this.newObj;
// Student特有属性
this.course = course;
}
Student.prototype = Object.create(Person.prototype);
// 设置"constructor" 属性指向Student
Student.prototype.constructor = Student;
// 更改Person中toString方法
Student.prototype.toString = function() {
document.write(this.name + " " + this.course);
}