es6实现继承的原理

  • 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 =
	/*#__PURE__*/
	function() {
		function A(age) {
			_classCallCheck(this, A);

			this.age = age;
		}

		_createClass(A, [{
			key: "fn",
			value: function fn() {}
		}]);

		return A;
	}();

var B =
	/*#__PURE__*/
	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(){}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值