class类通过extends继承父类

      class Person {
        constructor(name) {
          this.name = name
        }
        publicFn() {
          console.log('公共方法')
        }
      }
      class Student extends Person {
        constructor(name, score) {
          // this.name = name
          super(name)
          this.score = score
        }
        introduce() {
          console.log(`我是${this.name},考了${this.score}分`)
        }
      }
      class Teacher extends Person {
        constructor(name, subject) {
          // this.name = name
          super(name)
          this.subject = subject
        }
        teach() {
          console.log(`我是${this.name},教${this.subject}`)
        }
      }
      const stu = new Student('小明', 100)
      const teac = new Teacher('王老师', '语文')
      console.log(stu)
      stu.introduce()
      stu.publicFn()
      console.log(teac)
      teac.teach()
      teac.publicFn()

原文地址:https://www.cnblogs.com/wuqilang/p/15167450.html