实现
通过原型链来实现继承
例子
<script>
// 父类,人类
function People(name, age, sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
People.prototype.sayHello = function () {
console.log('你好,我是' + this.name + '我今年' + this.age + '岁了');
};
People.prototype.sleep = function () {
console.log(this.name + '开始睡觉,zzzzz');
};
// 子类,学生类
function Student(name, age, sex, scholl, studentNumber) {
this.name = name;
this.age = age;
this.sex = sex;
this.scholl = scholl;
this.studentNumber = studentNumber;
};
// 关键语句,实现继承
Student.prototype = new People();
Student.prototype.study = function () {
console.log(this.name + '正在学习');
};
Student.prototype.exam = function () {
console.log(this.name + '正在考试,加油!');
};
// 实例化
var hanmeimei = new Student('韩梅梅', 9, '女', '慕课小学', 100556);
hanmeimei.study();
hanmeimei.sayHello();
hanmeimei.sleep();
</script>
并且子类是可以重写父类的方法的
// 重写、复写(override)父类的sayHello
Student.prototype.sayHello = function () {
console.log('敬礼!我是' + this.name + '我今年' + this.age + '岁了');
}