ES6的 super 关键字

Class之间能够经过extends关键字实现继承,这比ES5的经过修改原型链实现继承,要清晰和方便不少。
浏览器

子类必须在constructor方法中调用super方法,不然新建实例时会报错。bash

这是由于子类没有本身的this对象,而是继承父类的this对象
,而后对其进行加工。若是不调用super方法,子类就得不到this对象。


ES5的继承,实质是先创造子类的实例对象this,而后再将父类的方法添加到this上面(Parent.apply(this))。ES6的继承机制彻底不一样,实质是先创造父类的实例对象this(因此必须先调用super方法),而后再用子类的构造函数修改this。
app


在子类的构造函数中,只有调用super以后,才可使用this关键字,不然会报错。这是由于子类实例的构建,是基于对父类实例加工,只有super方法才能返回父类实例。
函数

class ColorPoint extends Point {
  constructor(x, y, color) {
    super(x, y); // 调用父类的constructor(x, y)
    this.color = color;
  }
  toString() {
    return this.color + ' ' + super.toString(); // 调用父类的toString()
  }
}




class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
}
class ColorPoint extends Point {
  constructor(x, y, color) {
    this.color = color; // ReferenceError
    super(x, y);
    this.color = color; // 正确
  }
}



let cp = new ColorPoint(25, 8, 'green');
cp instanceof ColorPoint // true
cp instanceof Point // true复制代码

类的prototype属性和proto属性
ui

大多数浏览器的ES5实现之中,每个对象都有proto属性,指向对应的构造函数的prototype属性。Class做为构造函数的语法糖,同时有prototype属性和proto属性,所以同时存在两条继承链。this

1)子类的proto属性,表示构造函数的继承,老是指向父类。spa

2)子类prototype属性的proto属性,表示方法的继承,老是指向父类的prototype属性。prototype


class A {
}
class B extends A {
}
B.__proto__ === A // true
B.prototype.__proto__ === A.prototype // true
复制代码

Object.getPrototypeOf()
code

Object.getPrototypeOf方法能够用来从子类上获取父类。可使用这个方法判断,一个类是否继承了另外一个类。
对象

Object.getPrototypeOf(ColorPoint) === Point
// true
复制代码

Class的取值函数(getter)和存值函数(setter)

与ES5同样,在Class内部可使用get和set关键字,对某个属性设置存值函数和取值函数,拦截该属性的存取行为。

class MyClass {
  constructor() {
    // ...
  }
  get prop() {
    return 'getter';
  }
  set prop(value) {
    console.log('setter: '+value);
  }
}
let inst = new MyClass();
inst.prop = 123;
// setter: 123
inst.prop
// 'getter'
复制代码

Class不存在变量提高(hoist),这一点与ES5彻底不一样。

new Foo(); // ReferenceError
class Foo {}
复制代码

Class当即执行表达式

采用Class表达式,能够写出当即执行的Class。

let person = new class {
  constructor(name) {
    this.name = name;   //类的方法内部若是含有this,它默认指向类的实例
  }
  sayName() {
    console.log(this.name);
  }
}('张三');
person.sayName(); // "张三"
复制代码



super关键字:


super这个关键字,既能够看成函数使用,也能够看成对象使用。在这两种状况下,它的用法彻底不一样。
1) super做为函数调用时,表明父类的构造函数。ES6 要求,子类的构造函数必须执行一次super函数。

class A {}
class B extends A {
  constructor() {
    super();   //实例化父类
  }
}复制代码

注意,super虽然表明了父类A的构造函数,可是返回的是子类B的实例,即super内部的this指的是B,所以super()在这里至关于

A.prototype.constructor.call(this)

2) super做为对象时,在普通方法中,指向父类的原型对象;在静态方法中,指向父类。

class A {
  p() {
    return 2;
  }
}
class B extends A {
  constructor() {
    super();
    console.log(super.p()); // 2
    //super.p() 至关于 A.prototype.p()
  }
}
let b = new B();复制代码

因为super指向父类的原型对象,因此定义在父类实例上的方法或属性,是没法经过super调用的。

class A {
  constructor() {
    this.p = 2;
  }
}
class B extends A {
  get m() {
    return super.p;
  }
}
let b = new B();
b.m // undefined复制代码

若是属性定义在父类的原型对象上,super就能够取到。

class A {}
A.prototype.x = 2;
class B extends A {
  constructor() {
    super();
    console.log(super.x) // 2
  }
}
let b = new B();复制代码

ES6 规定,经过super调用父类的方法时,super会绑定子类的this。

class A {
  constructor() {
    this.x = 1;
  }
  print() {
    console.log(this.x);
  }
}
class B extends A {
  constructor() {
    super();
    this.x = 2;
  }
  m() {
    super.print();
  }
}
let b = new B();
b.m() // 2复制代码

上面代码中,super.print()虽然调用的是A.prototype.print(),可是A.prototype.print()会绑定子类B的this,致使输出的是2,而不是1。也就是说,实际上执行的是super.print.call(this)。

相关文章
相关标签/搜索