js bable怎么做的继承

先用es6写个简单的继承

class A {
  constructor() {
    this.name = 'AA';
  }
  getName() {
    return this.name;
  }
}

class B extends A {
  constructor() {
    super();
    this.age = 'b-age';
    const bdata = 1;
  }

  getAge() {
    return this.age + super.name;
  }
}

const d = new B();

bable编译后的代码

'use strict';

var _get = function get(object, property, receiver) {
  if (object === null) object = Function.prototype;
  var desc = Object.getOwnPropertyDescriptor(object, property);
  if (desc === undefined) {
    var parent = Object.getPrototypeOf(object);
    if (parent === null) {
      return undefined;
    } else {
      return get(parent, property, receiver);
    }
  } else if ("value" in desc) {
    return desc.value;
  } else {
    var getter = desc.get;
    if (getter === undefined) {
      return undefined;
    }
    return getter.call(receiver);
  }
};

var _createClass = function () {
  function defineProperties(target, props) {
    for (var i = 0; i < props.length; i++) {
      var descriptor = props[i];
      descriptor.enumerable = descriptor.enumerable || false;
      descriptor.configurable = true;
      if ("value" in descriptor) descriptor.writable = true;
      Object.defineProperty(target, descriptor.key, descriptor);
    }
  }
  return function (Constructor, protoProps, staticProps) {
    if (protoProps) defineProperties(Constructor.prototype, protoProps);
    if (staticProps) defineProperties(Constructor, staticProps);
    return Constructor;
  };
}();

function _possibleConstructorReturn(self, call) {
  if (!self) {
    throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  }
  return call && (typeof call === "object" || typeof call === "function") ? call : self;
}

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;
}

function _classCallCheck(instance, Constructor) {
  if (!(instance instanceof Constructor)) {
    throw new TypeError("Cannot call a class as a function");
  }
}

var A = function () {
  function A() {
    _classCallCheck(this, A);

    this.name = 'AA';
  }

  _createClass(A, [{
    key: 'getName',
    value: function getName() {
      return this.name;
    }
  }]);

  return A;
}();

var B = function (_A) {
  _inherits(B, _A);

  function B() {
    _classCallCheck(this, B);

    var _this = _possibleConstructorReturn(this, (B.__proto__ || Object.getPrototypeOf(B)).call(this));

    _this.age = 'b-age';
    var bdata = 1;
    return _this;
  }

  _createClass(B, [{
    key: 'getAge',
    value: function getAge() {
      return this.age + _get(B.prototype.__proto__ || Object.getPrototypeOf(B.prototype), 'name', this);
    }
  }]);

  return B;
}(A);

var d = new B();

通过_createClass()构造类,_inherits函数做继承(组合继承)。
针对_inherits我说两个点:1. subClass.prototype得到的是一个新对象,此对象的_ _proto_ _指向的是superClass.prototype, 这样原型链就串起来了。2. subClass.__proto__ = superClass的目的就是组合继承中通过call来获取父类的私有属性。

你可能感兴趣的:(js bable怎么做的继承)