es6继承

  • 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;大一
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容