編譯前express
// 父類 class Fruit { static nutrition = "vitamin" static plant() { console.log('種果樹'); } name; constructor(name) { this.name = name; } hello() { console.log(this.name); } } // 子類 class Mongo extends Fruit { constructor(name, level) { super(name); this.level = level; } eat() { console.log(super.name, this.name); } }
編譯後函數
"use strict"; // 獲取類型 function _typeof(obj) { // 運行環境原生支持Symbol if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } // 模擬實現Symbol else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } /** * 可能調用父類的構造函數,返回了建立的事物 * @param {Object} self - 上下文 * @param {Object} call - 調用父構造函數,建立的事務(對象/函數) */ function _possibleConstructorReturn(self, call) { // 語法規則:構造函數經過new運算符調用時,只能返回對象/函數 if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } // 斷言上下文已被初始化 function _assertThisInitialized(self) { // 沒有調用super初始化上下文,拋異常 if (self === void 0) { throw new ReferenceError( "this hasn't been initialised - super() hasn't been called" ); } return self; } // 讀取屬性 function _get(target, property, receiver) { // 支持Reflect if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } // 模擬Relect else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); } // 沿着原型鏈向上查找,直到找到最先擁有該屬性的原型對象(即不是經過繼承得到該屬性) function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; } // 讀取__proto__ 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"); } // 繼承成員方法:子構造函數的prototype,繼承父構造函數的prototype subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); // 繼承靜態屬性、靜態方法:子構造函數的__proto__,是父構造函數 if (superClass) _setPrototypeOf(subClass, superClass); } // 設置__proto__ 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; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var Fruit = /*#__PURE__*/ (function () { _createClass(Fruit, null, [ { key: "plant", value: function plant() { console.log("種果樹"); } } ]); function Fruit(name) { _classCallCheck(this, Fruit); _defineProperty(this, "name", void 0); this.name = name; } _createClass(Fruit, [ { key: "hello", value: function hello() { console.log(this.name); } } ]); return Fruit; })(); _defineProperty(Fruit, "nutrition", "vitamin"); var Mongo = /*#__PURE__*/ (function (_Fruit) { _inherits(Mongo, _Fruit); function Mongo(name, level) { var _this; _classCallCheck(this, Mongo); _this = _possibleConstructorReturn( this, _getPrototypeOf(Mongo).call(this, name) ); _this.level = level; return _this; } _createClass(Mongo, [ { key: "eat", value: function eat() { console.log( _get(_getPrototypeOf(Mongo.prototype), "name", this), this.name ); } } ]); return Mongo; })(Fruit);