- extend在实现继承方面,本质上也是原型链继承,不过相对于es5原有继承模式而言,多了一条原型链继承。
- 继承会首先在闭包里调用
_inherits(B, _A);
该方法实现了两步原型链继承
B.prototype = Object.create(A.prototype, {constructor:{value: B}})
Object.setPrototypeOf(B, A)
;即将B.__proto__ =A
- 然后在构造函数里进行初始化操作
- 首先调用
_classCallCheck
来阻止直接函数调用,原理通过判断当前this 是否是A的实例(这个是class定义函数的限制,不是extend的限制) - 调用
_this = _possibleConstructorReturn(this, _getPrototypeOf(B).call(this, age))
;_getPrototypeOf(B)相当于 A;此句相当于调用了A的构造函数;所以上面第二处原型链B.__proto__ =A
就得到了体现 - 调用
_createClass
为子类原型添加自定义方法
- 以上是普通函数继承,如果要是继承系统对象,比如说Date,在第一步_A是又另外包了一层的内部函数WrapperClass。
WrapperClass.prototype.__proto__
指向Date.prototype
。A子构造函数返回的是Date的对象b,b的原型指向WrapperClass
"use strict";
function _typeof(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function _typeof(obj) {
return typeof obj;
};
} else {
_typeof = function _typeof(obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" :
typeof obj;
};
}
return _typeof(obj);
}
function _possibleConstructorReturn(self, call) {
if (call && (_typeof(call) === "object" || typeof call === "function")) {
return call;
}
return _assertThisInitialized(self);
}
function _assertThisInitialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf(o);
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _setPrototypeOf(subClass, superClass);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
function _instanceof(left, right) {
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
return right[Symbol.hasInstance](left);
} else {
return left instanceof right;
}
}
function _classCallCheck(instance, Constructor) {
if (!_instanceof(instance, Constructor)) {
throw new TypeError("Cannot call a class as a 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);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
var A =
function() {
function A(age) {
_classCallCheck(this, A);
this.age = age;
}
_createClass(A, [{
key: "fn",
value: function fn() {}
}]);
return A;
}();
var B =
function(_A) {
_inherits(B, _A);
function B(name, age) {
var _this;
_classCallCheck(this, B);
_this = _possibleConstructorReturn(this, _getPrototypeOf(B).call(this, age));
_this.name = name;
return _this;
}
_createClass(B, [{
key: "bfn",
value: function bfn() {}
}]);
return B;
}(A);
function A(){
if (!( this instanceof A)) {
console.error('must be called by new')
}
}
class A{
constructor(age){
this.age=age;
}
fn(){}
}
class B extends A{
constructor(name, age){
this.name = name;
super(age)
}
bfn(){}
}