pass: 如果你的原型链理解还欠缺火候,这里不建议强行学习继承。 —by melodyWxy

es6 的 extends

我们由es6的extends表现去解读继承的含义

  1. class Parent {
  2. a = 'a';
  3. b(){
  4. console.log('b')
  5. }
  6. c = ()=> {
  7. console.log('c')
  8. }
  9. static d = 'd'
  10. }
  11. class Child extends Parent{
  12. e = 'e';
  13. f(){
  14. console.log('f')
  15. }
  16. g = ()=> {
  17. console.log(g)
  18. }
  19. }
  20. console.log(new Parent());
  21. console.dir(Child)
  22. console.log(new Child());

Bable 一下 extends

  1. "use strict";
  2. function _inheritsLoose(subClass, superClass) {
  3. subClass.prototype = Object.create(superClass.prototype);
  4. subClass.prototype.constructor = subClass;
  5. subClass.__proto__ = superClass;
  6. }
  7. var Parent = function Parent() {};
  8. var Child = /*#__PURE__*/function (_Paren) {
  9. _inheritsLoose(Child, _Paren);
  10. function Child() {
  11. return _Paren.apply(this, arguments) || this;
  12. }
  13. return Child;
  14. }(Paren);

封装一个继承

  1. function myExtends(ChildClass,ParentClass){
  2. // 有缺陷的。
  3. ChildClass.prototype = Object.create(ParentClass.prototype);
  4. ChildClass.prototype.constructor = ChildClass;
  5. ChildClass.__proto__ = ParentClass;
  6. }

原生继承方式优劣

https://www.cnblogs.com/ranyonsue/p/11201730.html