-
class
定义:可以理解为一个function,因此具有prototype属性,当new一个class时,会将class的prototype属性赋值给新对象proto属性
-
constructor
定义:默认添加的方法,在new一个class时,自动调用该方法,constructor中定义自己的属性
-
extends和super
定义:class 子类名 extends 父类名实现继承,在constructor中使用super(父类的参数);意思是获取父类的this指针,相当于Father.call(this)
-
实现
class Father{
constructor(name,age){
this.name=name;
this.age=age;
}
showAge(){
return this.age;
}
}
class Son extends Father{
constructor(name,age,grade){
super(name,age)
this.grade=grade;
}
showInfo(){
console.log(this.name+";"+super.showAge()+";"+this.grade)
//使用super+方法名使用父类方法
}
}
var son=new Son("zzc",11,"大一")
son.showInfo() //zzc;11;大一