ES6新特性class类(类继承)

75 阅读1分钟
class Person{
    constructor(name,age){
        this.name=name;
        this.age=age;
    }
    do(){
        console.log('类原型方法')
    }
    static study(){
        console.log('给父类添加静态方法');
    }
}
   //声明student继承于父类Person;但是student(子类)没有自己的this,需要super()重塑
class Student extends Person{
    constructor(sex,name,age){
        super();//给子类重塑this;
        this.sex=sex;
    }
    eat(){
        console.log('给子类原型添加方法');
    }
}
Student.prototype.study=function(){
    console.log('子类添加的原型方法,父类无法获取到');
}
const per=new Person();
const stu=new Student('小明','男',18);
console.log(stu.do===per.do); //true
console.log(per.study);//undefined

注意:class类也是new 调用