es6 class

    ES6的class可以看作只是一个语法糖,它的绝大部分功能,ES5都可以做到,新的class写法只是让对象原型的写法更加清晰、更像面向对象编程的语法而已。

function Point(x, y) {
  this.x = x;
  this.y = y;
}

Point.prototype.toString = function () {
  return '(' + this.x + ', ' + this.y + ')';
};
等同于

class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }

  toString() {
    return '(' + this.x + ', ' + this.y + ')';
  }
}

在 JavaScript 中,每个对象都有原型对象。所有 JavaScript 对象都从原型上继承方法和属性。
ES5中,属性放在构造函数(constructor)里,方法放在原型(prototype)上;
ES6中引入了类(class)来代替构造函数(constructor);

在之类的constructor中必须调用 super方法,子类没有自己的this对象,而是继承父类的this对象,然后对其进行加工。super代表了父类构造函数。对于你的实例相当于执行Component(props)。但是注意,此处this指向 子类。更严谨的是相当于

Component.prototype.constructor.call(this,props)。

至于为什么一定要有super?可以很容易想得到,先有父才有子嘛。super继承父类属性,在constructor中进行自己的改造。如果在constructor中使用到this,必须在使用this之前调用super()。

原文地址:https://www.cnblogs.com/lmyt/p/7049940.html