前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >es6类和继承的实现原理

es6类和继承的实现原理

作者头像
ConardLi
发布2019-09-08 14:42:26
1.6K0
发布2019-09-08 14:42:26
举报
文章被收录于专栏:code秘密花园code秘密花园

ConardLi code秘密花园 今天

导读:

分类:技术干货

题目:es6类和继承的实现原理

通过babel探索es6的语法糖

在阅读文章之前,您至少需要对JavaScript原型继承有一定了解,如果觉得有所欠缺,可以先了解下我这篇文章:原型和原型链

1.es6 class 使用

javascript使用的是原型式继承,我们可以通过原型的特性实现类的继承, es6为我们提供了像面向对象继承一样的语法糖。

代码语言:javascript
复制
class Parent {
  constructor(a){
    this.filed1 = a;
  }
  filed2 = 2;
  func1 = function(){}
}

class Child extends Parent {
    constructor(a,b) {
      super(a);
      this.filed3 = b;
    }

  filed4 = 1;
  func2 = function(){}
}

下面我们借助babel来探究es6类和继承的实现原理。

1.类的实现

转换前:

代码语言:javascript
复制
class Parent {
  constructor(a){
    this.filed1 = a;
  }
  filed2 = 2;
  func1 = function(){}
}

转换后:

代码语言:javascript
复制
function _classCallCheck(instance, Constructor) {
  if (!(instance instanceof Constructor)) {
    throw new TypeError("Cannot call a class as a function");
  }
}

var Parent = function Parent(a) {
  _classCallCheck(this, Parent);

  this.filed2 = 2;

  this.func1 = function () { };

  this.filed1 = a;
};

可见class的底层依然是构造函数:

1.调用_classCallCheck方法判断当前函数调用前是否有new关键字。

构造函数执行前有new关键字,会在构造函数内部创建一个空对象,将构造函数的proptype指向这个空对象的proto,并将this指向这个空对象。如上,_classCallCheck中:this instanceof Parent 返回true。 若构造函数前面没有new则构造函数的proptype不会不出现在this的原型链上,返回false。

2.将class内部的变量和函数赋给this

3.执行constuctor内部的逻辑

4.return this (构造函数默认在最后我们做了)。

2.继承实现

转换前:

代码语言:javascript
复制
class Child extends Parent {
    constructor(a,b) {
      super(a);
      this.filed3 = b;
    }

  filed4 = 1;
  func2 = function(){}
}

转换后:

我们先看Child内部的实现,再看内部调用的函数是怎么实现的:

代码语言:javascript
复制
var Child = function (_Parent) {
  _inherits(Child, _Parent);

  function Child(a, b) {
    _classCallCheck(this, Child);

    var _this = _possibleConstructorReturn(this, (Child.__proto__ || Object.getPrototypeOf(Child)).call(this, a));

    _this.filed4 = 1;

    _this.func2 = function () {};

    _this.filed3 = b;
    return _this;
  }

  return Child;
}(Parent);

1.调用_inherits函数继承父类的proptype。

_inherits内部实现:

代码语言:javascript
复制
function _inherits(subClass, superClass) {
  if (typeof superClass !== "function" && superClass !== null) {
    throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  }
  subClass.prototype = Object.create(superClass && superClass.prototype, {
    constructor: { value: subClass, enumerable: false, writable: true, configurable: true }
  });
  if (superClass)
    Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
}

(1) 校验父构造函数。

(2) 典型的寄生继承:用父类构造函数的proptype创建一个空对象,并将这个对象指向子类构造函数的proptype。

(3) 将父构造函数指向子构造函数的proto(这步是做什么的不太明确,感觉没什么意义。)

2.用一个闭包保存父类引用,在闭包内部做子类构造逻辑。

3.new检查。

4.用当前this调用父类构造函数。

代码语言:javascript
复制
var _this = _possibleConstructorReturn(this, (Child.__proto__ || Object.getPrototypeOf(Child)).call(this, a));

这里的Child.proto || Object.getPrototypeOf(Child)实际上是父构造函数(_inherits最后的操作),然后通过call将其调用方改为当前this,并传递参数。(这里感觉可以直接用参数传过来的Parent)

代码语言:javascript
复制
function _possibleConstructorReturn(self, call) {
  if (!self) {
    throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  }
  return call && (typeof call === "object" || typeof call === "function") ? call : self;
}

校验this是否被初始化,super是否调用,并返回父类已经赋值完的this。

5.将行子类class内部的变量和函数赋给this。

6.执行子类constuctor内部的逻辑。

可见,es6实际上是为我们提供了一个“组合寄生继承”的简单写法。

3. super

super代表父类构造函数。

super.fun1() 等同于 Parent.fun1() 或 Parent.prototype.fun1()。

super() 等同于Parent.prototype.construtor()

当我们没有写子类构造函数时:

代码语言:javascript
复制
var Child = function (_Parent) {
  _inherits(Child, _Parent);

  function Child() {
    _classCallCheck(this, Child);

    return _possibleConstructorReturn(this, (Child.__proto__ || Object.getPrototypeOf(Child)).apply(this, arguments));
  }

  return Child;
}(Parent);

可见默认的构造函数中会主动调用父类构造函数,并默认把当前constructor传递的参数传给了父类。

所以当我们声明了constructor后必须主动调用super(),否则无法调用父构造函数,无法完成继承。

典型的例子就是Reatc的Component中,我们声明constructor后必须调用super(props),因为父类要在构造函数中对props做一些初始化操作。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-01-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 code秘密花园 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.es6 class 使用
  • 1.类的实现
  • 2.继承实现
  • 3. super
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档