ES6类

1、通过关键字class来声明一个类

  class Animal {
        constructor(name,color){
            this.name=name;
            this.color=color;
            console.log("动物类");
        }
    }
    let dog=new Animal('旺财','黑白斑点');
    console.log(dog);

2、类的继承

  • constructor: 构造函数
  • this : 当前实例
  • super: 它的父类
class Animal {
  // 创建类的时候会执行
  constructor(color){
    this.color = color
  }
  
  eat(){
    console.log("动物吃饭")
  }
}

class Cat extends Animal{
  constructor(color){
    super(color)
  }
}

3、类的静态方法、属性

  1、通过static声明静态方法和属性

  2、静态方法和属性归属于类,只能通过类去调用和获取

  3、普通的方法归属于实例,需要通过实例调用

 class Animal {
        constructor(name, color) {
            this.name = name;
            this.color = color;
            console.log("动物类");
        }
        eat() {
            console.log('动物会进食');
        }
        static meat() {
            console.log('会吃肉');
            return this;
        }
    }

    let dog = new Animal('旺财', '黑白斑点');
    console.log(dog);
    console.log(Animal.meat());

 

posted @ 2020-09-03 21:37  靡荼  阅读(112)  评论(0编辑  收藏  举报