编译前
1// 父类 2class Fruit { 3 static nutrition = "vitamin" 4 static plant() { 5 console.log('种果树'); 6 } 7 name; 8 constructor(name) { 9 this.name = name; 10 } 11 hello() { 12 console.log(this.name); 13 } 14} 15 16// 子类 17class Mongo extends Fruit { 18 constructor(name, level) { 19 super(name); 20 this.level = level; 21 } 22 eat() { 23 console.log(super.name, this.name); 24 } 25}
编译后
1"use strict"; 2 3// 获取类型 4function _typeof(obj) { 5 // 运行环境原生支持Symbol 6 if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { 7 _typeof = function _typeof(obj) { 8 return typeof obj; 9 }; 10 } 11 // 模拟实现Symbol 12 else { 13 _typeof = function _typeof(obj) { 14 return obj && 15 typeof Symbol === "function" && 16 obj.constructor === Symbol && 17 obj !== Symbol.prototype 18 ? "symbol" 19 : typeof obj; 20 }; 21 } 22 return _typeof(obj); 23} 24 25/** 26 * 可能调用父类的构造函数,返回了创建的事物 27 * @param {Object} self - 上下文 28 * @param {Object} call - 调用父构造函数,创建的事务(对象/函数) 29 */ 30function _possibleConstructorReturn(self, call) { 31 // 语法规则:构造函数通过new运算符调用时,只能返回对象/函数 32 if (call && (_typeof(call) === "object" || typeof call === "function")) { 33 return call; 34 } 35 return _assertThisInitialized(self); 36} 37 38// 断言上下文已被初始化 39function _assertThisInitialized(self) { 40 // 没有调用super初始化上下文,抛异常 41 if (self === void 0) { 42 throw new ReferenceError( 43 "this hasn't been initialised - super() hasn't been called" 44 ); 45 } 46 return self; 47} 48 49// 读取属性 50function _get(target, property, receiver) { 51 // 支持Reflect 52 if (typeof Reflect !== "undefined" && Reflect.get) { 53 _get = Reflect.get; 54 } 55 // 模拟Relect 56 else { 57 _get = function _get(target, property, receiver) { 58 var base = _superPropBase(target, property); 59 if (!base) return; 60 var desc = Object.getOwnPropertyDescriptor(base, property); 61 if (desc.get) { 62 return desc.get.call(receiver); 63 } 64 return desc.value; 65 }; 66 } 67 return _get(target, property, receiver || target); 68} 69 70// 沿着原型链向上查找,直到找到最早拥有该属性的原型对象(即不是通过继承获得该属性) 71function _superPropBase(object, property) { 72 while (!Object.prototype.hasOwnProperty.call(object, property)) { 73 object = _getPrototypeOf(object); 74 if (object === null) break; 75 } 76 return object; 77} 78 79// 读取__proto__ 80function _getPrototypeOf(o) { 81 _getPrototypeOf = Object.setPrototypeOf 82 ? Object.getPrototypeOf 83 : function _getPrototypeOf(o) { 84 return o.__proto__ || Object.getPrototypeOf(o); 85 }; 86 return _getPrototypeOf(o); 87} 88 89// 继承 90function _inherits(subClass, superClass) { 91 if (typeof superClass !== "function" && superClass !== null) { 92 throw new TypeError("Super expression must either be null or a function"); 93 } 94 // 继承成员方法:子构造函数的prototype,继承父构造函数的prototype 95 subClass.prototype = Object.create(superClass && superClass.prototype, { 96 constructor: { value: subClass, writable: true, configurable: true } 97 }); 98 // 继承静态属性、静态方法:子构造函数的__proto__,是父构造函数 99 if (superClass) _setPrototypeOf(subClass, superClass); 100} 101 102// 设置__proto__ 103function _setPrototypeOf(o, p) { 104 _setPrototypeOf = 105 Object.setPrototypeOf || 106 function _setPrototypeOf(o, p) { 107 o.__proto__ = p; 108 return o; 109 }; 110 return _setPrototypeOf(o, p); 111} 112 113function _instanceof(left, right) { 114 if ( 115 right != null && 116 typeof Symbol !== "undefined" && 117 right[Symbol.hasInstance] 118 ) { 119 return !!right[Symbol.hasInstance](left); 120 } else { 121 return left instanceof right; 122 } 123} 124 125function _classCallCheck(instance, Constructor) { 126 if (!_instanceof(instance, Constructor)) { 127 throw new TypeError("Cannot call a class as a function"); 128 } 129} 130 131function _defineProperties(target, props) { 132 for (var i = 0; i < props.length; i++) { 133 var descriptor = props[i]; 134 descriptor.enumerable = descriptor.enumerable || false; 135 descriptor.configurable = true; 136 if ("value" in descriptor) descriptor.writable = true; 137 Object.defineProperty(target, descriptor.key, descriptor); 138 } 139} 140 141function _createClass(Constructor, protoProps, staticProps) { 142 if (protoProps) _defineProperties(Constructor.prototype, protoProps); 143 if (staticProps) _defineProperties(Constructor, staticProps); 144 return Constructor; 145} 146 147function _defineProperty(obj, key, value) { 148 if (key in obj) { 149 Object.defineProperty(obj, key, { 150 value: value, 151 enumerable: true, 152 configurable: true, 153 writable: true 154 }); 155 } else { 156 obj[key] = value; 157 } 158 return obj; 159} 160 161var Fruit = 162 /*#__PURE__*/ 163 (function () { 164 _createClass(Fruit, null, [ 165 { 166 key: "plant", 167 value: function plant() { 168 console.log("种果树"); 169 } 170 } 171 ]); 172 173 function Fruit(name) { 174 _classCallCheck(this, Fruit); 175 176 _defineProperty(this, "name", void 0); 177 178 this.name = name; 179 } 180 181 _createClass(Fruit, [ 182 { 183 key: "hello", 184 value: function hello() { 185 console.log(this.name); 186 } 187 } 188 ]); 189 190 return Fruit; 191 })(); 192 193_defineProperty(Fruit, "nutrition", "vitamin"); 194 195var Mongo = 196 /*#__PURE__*/ 197 (function (_Fruit) { 198 _inherits(Mongo, _Fruit); 199 200 function Mongo(name, level) { 201 var _this; 202 203 _classCallCheck(this, Mongo); 204 205 _this = _possibleConstructorReturn( 206 this, 207 _getPrototypeOf(Mongo).call(this, name) 208 ); 209 _this.level = level; 210 return _this; 211 } 212 213 _createClass(Mongo, [ 214 { 215 key: "eat", 216 value: function eat() { 217 console.log( 218 _get(_getPrototypeOf(Mongo.prototype), "name", this), 219 this.name 220 ); 221 } 222 } 223 ]); 224 225 return Mongo; 226 })(Fruit);