extends
2023-03-23 本文已影响0人
欢西西西
// class Drinks {
// constructor(capacity) {
// this.capacity = capacity;
// }
// }
// class Coke extends Drinks {
// constructor(capacity) {
// super(capacity);
// }
// }
// #region ES5
function Drinks(capacity) {
this.capacity = capacity;
}
function Coke() {
Drinks.call(this, ...arguments);
}
Coke.prototype = Object.create(Drinks.prototype, {
constructor: { value: Coke }
}); // Coke.prototype.__proto__ === Drinks.prototype
Object.setPrototypeOf(Coke, Drinks); // Coke.__proto__ === Function.prototype -> Coke.__proto__ === Drinks
// Object.create
// |__创建一个空对象,这个对象的原型对象指向Drinks.prototype
// |__第二个参数是生成的这个对象的属性描述,对应Object.defineProperties() 的第二个参数
// #endregion
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
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, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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 }
});
Object.defineProperty(subClass, "prototype", { writable: false });
if (superClass) _setPrototypeOf(subClass, superClass);
// Coke.__proto__ === Function.prototype ---→ Coke.__proto__ === Drinks
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf
? Object.setPrototypeOf.bind()
: function _setPrototypeOf(o, p) {
o.__proto__ = p; return o;
};
return _setPrototypeOf(o, p);
}
function _createSuper(Derived) {
var hasNativeReflectConstruct = _isNativeReflectConstruct();
return function _createSuperInternal() {
var Super = _getPrototypeOf(Derived), // 获取Coke的原型对象——Drinks构造函数
result;
if (hasNativeReflectConstruct) {
var NewTarget = _getPrototypeOf(this).constructor; // 当前实例的构造函数——Coke
result = Reflect.construct(Super, arguments, NewTarget);
/**
* https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Reflect/construct
*
* [参数1]被运行的构造函数
* [参数3]作为新创建对象的原型对象的 constructor 属性
*
* result = new Super(...arguments);
* result.__proto__.constructor = NewTarget;
* 表示result是从NewTarget创建的,而不是Super创建的
* 优先通过Reflect.construct去调用构造函数,保证new.target不丢失
* 通过apply调用构造函数的时候,new.target是undefined
*/
} else {
result = Super.apply(this, arguments); // 执行完Super返回undefined
}
/**
* 第1种:调用构造函数创建的对象result,没有修改this这个实例,result一定有值
* 第2种:修改了this,result是构造函数的显式返回值,值可能有或没有
*/
// 父类构造函数执行完如果有显式的返回值
return _possibleConstructorReturn(this, result);
};
}
function _possibleConstructorReturn(self, call) {
if (call && (_typeof(call) === "object" || typeof call === "function")) {
return call;
} else if (call !== void 0) {
throw new TypeError("Derived constructors may only return object or undefined");
}
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 _isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct)
return false;
if (Reflect.construct.sham)
return false;
if (typeof Proxy === "function")
return true;
try {
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () { }));
return true;
} catch (e) {
return false;
}
}
function _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf
?
Object.getPrototypeOf.bind()
:
function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf(o);
}
var Coke = /*#__PURE__*/function (_Drinks) {
_inherits(Coke, _Drinks); // 实现对父类原型链属性的继承
var _super = _createSuper(Coke); // 返回值可以先理解为父类的构造函数 _createSuper返回1个函数,执行后返回1个对象
function Coke(capacity) {
_classCallCheck(this, Coke);
return _super.call(this, capacity); // 注意这个构造函数的显式return
}
return _createClass(Coke);
}(Drinks);