Why set _ _ ptoto__? to the subclass constructor when babel transcodes the inheritance syntax of es6

function _inherits(subClass, superClass) {
  if (typeof superClass !== "function" && superClass !== null) {
    throw new TypeError(
      "Super expression must either be null or a function, not " +
        typeof superClass
    );
  }
  subClass.prototype = Object.create(superClass && superClass.prototype, {
    constructor: {
      value: subClass,
      enumerable: false,
      writable: true,
      configurable: true
    }
  });
  if (superClass)
    Object.setPrototypeOf
      ? Object.setPrototypeOf(subClass, superClass)
      : (subClass.__proto__ = superClass);
}

above is the transcoding effect of babel, where this code

if (superClass)
    Object.setPrototypeOf
      ? Object.setPrototypeOf(subClass, superClass)
      : (subClass.__proto__ = superClass);

question: 1. I think this code can be regarded as implementation inheritance without adding it. It seems that there is no code to set _ _ proto__ to the constructor of the subclass in the elevation. What"s the difference between setting and not setting?

  1. _ _ proto__ remember that there are only objects? Although everything in js is an object, how do you understand the _ _ proto__ of its _ _ proto__, function in an instance of the common story new?
Mar.28,2021

answer: understand by looking up the data

if (superClass)
    Object.setPrototypeOf
      ? Object.setPrototypeOf(subClass, superClass)
      : (subClass.__proto__ = superClass);

this code is to inherit the static properties of the parent class, which are generally added to the constructor rather than the prototype.

Menu