js中继承的实现
function People(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
}
People.prototype.changge = function(){
console.log(`我是一个${this.name},我今年${this.age}岁了`);
}
function Student(name,age,sex,xuehao,banji){
this.name = name;
this.age = age;
this.sex = sex;
this.xuehao = xuehao;
this.banji = banji;
}
Student.prototype = new People();
Student.prototype.kaoshi = function(){
console.log(`${this.name}在考试`);
}
var xiaohua = new Student("小花",12,"女",10001,"1班");
xiaohua.changge(); //原型链查找
xiaohua.kaoshi();
ES6中简化了类的继承
class People{
constructor(name , age , sex){
this.name = name;
this.age = age;
this.sex = sex;
}
changge(){
console.log("我是一个" + this.name + "今年" + this.age + "岁啦!!");
}
goDie(){
console.log("死啦");
}
}
class Student extends People{
constructor(name , age , sex , xuehao , banji){
super(name , age , sex); //调用超类的构造器
this.xuehao = xuehao;
this.banji = banji;
}
kaoshi(){
console.log(`${this.name}在考试`);
}
}
var xiaohua = new Student("小花",12,"女",10001,"1班");
xiaohua.changge();
xiaohua.kaoshi();
super():父类的构造函数
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。