原型对象(二)类与继承

ES6类的基本语法

例:

  class Cat {
      constructor(name,age){
          this.name = name
          this.age = age
      }
  }
  Cat.prototype.eat = function() {
      console.log("吃鱼")
  }
  let cat = new Cat("喵喵",2)
  cat.eat()
继承

例:

  class User{
      constructor(username,password){
          this.username = username
          this.password = password
      }
      login(){
          console.log("登录")
      }
  }

  class Admin extends User{
      deletePerson(){
          console.log("删除一个用户")
      }
  }

  let admin = new Admin()
  admin.deletePerson()
posted @ 2021-07-21 22:07  `Duet`  阅读(29)  评论(0编辑  收藏  举报