pass: 如果你的原型链理解还欠缺火候,这里不建议强行学习继承。 —by melodyWxy
es6 的 extends
我们由es6的extends表现去解读继承的含义
class Parent {a = 'a';b(){console.log('b')}c = ()=> {console.log('c')}static d = 'd'}class Child extends Parent{e = 'e';f(){console.log('f')}g = ()=> {console.log(g)}}console.log(new Parent());console.dir(Child)console.log(new Child());
Bable 一下 extends
"use strict";function _inheritsLoose(subClass, superClass) {subClass.prototype = Object.create(superClass.prototype);subClass.prototype.constructor = subClass;subClass.__proto__ = superClass;}var Parent = function Parent() {};var Child = /*#__PURE__*/function (_Paren) {_inheritsLoose(Child, _Paren);function Child() {return _Paren.apply(this, arguments) || this;}return Child;}(Paren);
封装一个继承
function myExtends(ChildClass,ParentClass){// 有缺陷的。ChildClass.prototype = Object.create(ParentClass.prototype);ChildClass.prototype.constructor = ChildClass;ChildClass.__proto__ = ParentClass;}
