ES6实现继承

浅谈es6中利用class进行继承

    主要利用class配合extendssuper实现继承;

首先利用class构造一个父类

class Person {
//构造函数实现属性定义

constructor(name,age){

    this.name = name
    this.age =age
}
//原型上的方法
say(){
    console.log(this.name)
}
复制代码

}

然后利用extends与super实现子类继承

class User extends Person {

constructor(name,age,tel){
    super(name,age);
    this.tel = tel;
}
call(){
    console.log(this.tel)
}
复制代码

}

const user = new User('lily',18,1234567);

console.log(user.name);//lily

user.say()//1234567

转载于:https://juejin.im/post/5c8f0a99e51d45633d746380

你可能感兴趣的:(ES6实现继承)