ES6类的使用

65 阅读2分钟

constructor方法是Class类的默认方法,通过new命令,创建一个新的实例对象时,会自动调用此方法。

一个类必须有constructor()方法,如果创建类的时候,没有显示的定义该方法,一个空的 constructor()方法会被默认添加。

class Wangpa{

}

//等同于
class Wangpa{
    constructor(){}
}

class实现继承和逻辑复用 super关键字

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
    this._address = "广州";
  }

  //普通的实例方法,通过创建出来的对象进行访问
  eating() {
    console.log(this.name + "eating");
  }

  running() {
    console.log(this.name + "running");
  }
    
  personMethod() {
    console.log("处理逻辑1");
    console.log("处理逻辑2");
    console.log("处理逻辑3");
  }

  //类的访问器方法
  get address() {
    console.log("拦截器操作");
    return this._address;
  }

  set address(newValue) {
    console.log("拦截设置操作");
    this._address = newValue;
  }

  //类的静态方法,类的静态方法通过类名去访问,也是类方法
  static personStaticMethod() {
    console.log("父类的静态方法");
  }
}

class Student extends Person {
  //js引擎在解析子类的时候就有要求,如果要实现继承,
  //那么子类的构造方法中,在使用this之前,要使用super
  constructor(name, age, son) {
    super(name, age);
    this.son = son;
  }

  studying() {
    console.log(this.name + "studying");
  }

  //子类对父类方法的重写
  runing() {
    console.log("student " + this.name + " runing");
  }

  stuMethod() {
    //复用父类中的处理逻辑
    super.personMethod();

    console.log("处理逻辑4");
    console.log("处理逻辑5");
    console.log("处理逻辑6");
  }

  //重写静态方法
  static staticMethod() {
    super.personStaticMethod();
    console.log("子类中的静态方法");
  }
}
  1. super作为函数调用时,代表父类的构造函数,内部的this指向的是子类的实例。作为函数调用时,只能在子类的构造函数中,用在其他地方会报错

    class A {
      constructor() {
        this.show();
      }
    }
    class B extends A {
      constructor() {
        super();
      }
      show(){
        console.log('实例');
      }
      static show(){
        console.log('子类');
      }
    }
    new B() //输出 实例
    
  2. super作为对象时,在普通方法中,指向父类的原型对象。即Class A类中,p方法保存在A的原型对象上

    class A {
      p() {
        return 2;
      }
    }
    class B extends A {
      constructor() {
        super();
        console.log(super.p()); // 2  此时的super指向父类原型对象,即 A.prototype
      }
    }
    let b = new B();
    
  3. super在constructor构造函数中,例如super.y = 123,此时super指向的是实例中的this,赋值的属性会变成子类实例的属性

    class A {
      constructor() {
        this.x = 1;
      }
      print() {
        console.log(this.x);
      }
    }
    class B extends A {
      constructor() {
        super();
        this.x = 2;
      super.y = 123;  
      }
      m() {
        super.print();
      }
    }
    let b = new B();
    b.m() // 2
    console.log(b.y);  //123
    
  4. 在静态方法中,super将指向父类,而不是父类的原型对象

    class Parent {
      static myMethod(msg) {
        console.log('static', msg);
      }
      myMethod(msg) {
        console.log('instance', msg);
      }
    }
    class Child extends Parent {
      static myMethod(msg) {
        super.myMethod(msg);
      }
      myMethod(msg) {
        super.myMethod(msg);
      }
    }
    Child.myMethod(1); 
    //此时会调用子类中的静态方法,super指向的父类,
    //即父类中静态方法myMethod被调用  输出为 static 1
    
    var child = new Child();
    child.myMethod(2); //这里myMethod为普通的实例方法 输出 instance 2
    
  5. 在子类的静态方法中通过super调用父类的方法,方法内部的this指向当前的子类,而不是子类的实例

    class A {
      constructor() {
        this.x = 1;
      }
      static print() {
        console.log(this.x);
      }
    }
    class B extends A {
      constructor() {
        super();
        this.x = 2;
      }
      static m() {
        super.print();
      }
    }
    B.x = 3;
    B.m() // 3