ES6 类的继承

来源:互联网 发布:淘宝网夏季竹凉席坐垫 编辑:程序博客网 时间:2024/05/29 13:49

//父类 people.js

class people{    constructor(name,age= 99){        this.name = name        this.age = age      }    sayHi(){      console.log("你好我叫" + this.name + ",今年:" + this.age + "岁了")    }}export {people}

//子类 student.js

import people from 'people.js'class student extends people{    constructor(name){        super(name)        this.name = name    }    sayHello(){        console.log("Hello My name is " + this.name)    }}export{student}

//index.js

import student from 'student.js'import people from 'people.js'onLoad(){    var s= new student('jack')    s.sayHello()   //"Hello My name is jack    s.sayHi()   //"你好我叫jack,今年99岁了"    var p = new people('tom','22')    p.sayHi()   //"你好我叫tom,今年22岁了"}