面向对象 - js [ES6]

面向对象

  • class 类声明
  • constructor 构造函数
  • extends 继承
  • super 父类/超类

class Person{
  constructor(name, age){
    this.name = name;
    this.age = age;
  }

  showName(){
    alert(this.name)
  }

   showAge(){
     alert(this.age)
   }
}

继承

class Worker extends Person{
   constructor(name, age, job){
      super(name, age)
      this.job = job
   }

    showJob(){
      alert(this.job)
    }
}

let w = new Worker('blue', 18, '...')

w.showName()// blue
w.showAge()// 18
posted @ 2021-08-16 21:35  独舟者  阅读(41)  评论(0)    收藏  举报