使用es6的面向对象实现继承

代码如下

//声明一个类
class People{
    constructor(name,age){
        this.name=name;
        this.age=age;
    }
    say(){
        console.log(`大家好我是${this.name},今年${this.age}岁!`)//es6语法的模板字符串
    }
}
 var person = new People('邱权武',18)//新建People对象
 person.say()//调用方法
 //类的继承
class Student extends People{
    constructor(name,age,score){
        super(name,age)
        this.score=score
    }
    say(){
        console.log(`大家好我是${this.name},今年${this.age}岁,我这次的成绩是${this.score}!`)
    }
}
var student =new Student('小王',23,98)
student.say()

你可能感兴趣的:(前端开发,Javascript)