您的位置:首页 > Web前端 > JavaScript

js 继承并重写方法示例

2016-09-09 15:14 288 查看
用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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: