ES6 javascript类继承,为什么我们需要从派生类调用super() [英] ES6 javascript class inheritance, why we need call to super() from derived class

查看:170
本文介绍了ES6 javascript类继承,为什么我们需要从派生类调用super()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript ES6中,在继承中,

如果派生类有构造函数,为什么必须从派生构造函数调用super?

In javascript ES6, in inheritance,
if derived class has constructor, why it is mandatory to call super from derived constructor ?

少数失败的例子是 - :

few failed examples are -:

。基础与构造函数但派生不调用super -

. Base with constructor but derived not calling super-

 class Base{constructor(){}}
    class Derived{constructor(){}}
    var d = new Derived(); // fails - ReferenceError: this is not defined


推荐答案


...似乎必须在基类中使用构造函数。

...it seems it is mandatory to have constructor function in base class.

不是真的。如果您没有提供,请一个将由JavaScript引擎提供给您。因此总会有一个(在这个意义上它是强制性的),但它不必显式编码。

Not really. If you don't provide one, one will be provided for you by the JavaScript engine. So there will always be one (it's mandatory in that sense), but it doesn't have to be coded explicitly.

当你根本没有定义一个构造函数时,JavaScript引擎为基类提供的默认值如下所示:

When you don't define a constructor at all, the default one provided by the JavaScript engine for a base class will look like this:

constructor( ) { }

...派生类中的默认值如下所示:

...and the default one in a derived class will look like this:

constructor(...args) {
    super(...args);
}

您的示例失败的原因是派生有一个显式的构造函数,但构造函数不会调用 super 。如果您明确定义一个,必须派生的构造函数中调用 super

The reason your example fails is that Derived has an explicit constructor, but that constructor doesn't call super. You must call super from within Derived's constructor if you explicitly define one.


如果派生类有构造函数,为什么必须从派生构造函数调用super?

if derived class has constructor, why it is mandatory to call super from derived constructor ?

因为您需要为超类提供对其必须执行的新对象进行任何初始化的机会。否则,超类不能保证它能正常工作,因为它可能依赖于其构造函数完成的初始化。

Because you are required to give the superclass an opportunity to do any initialization of the new object that it has to do. Otherwise, the superclass can't guarantee that it will work correctly, as it may rely on the initialization done by its constructor.

所以:


  1. Derived 中删除​​构造函数 ,使它像你的第一个例子,以便JavaScript引擎将提供默认构造函数,或

  1. Remove your constructor from Derived, making it like your first example so that the JavaScript engine will provide the default constructor, or

调用 super 来自派生的构造函数。

你问的评论:


但是如果基类没有任何构造函数,它仍然会失败,如果派生类有

but if base class does not have any constructor it still fails, if a derived class has

基类总是有一个构造函数,因为如果你没有提供一个(你在问题的代码中做了) ,提供默认值。所以你仍然需要打电话。虽然如果没有超类没有非默认构造函数,可以被指定为可选项,那就会增加复杂性并使派生's显式构造函数误导(不调用 super )。

The base class always has a constructor, because if you don't provide one (you did in the code in your question), a default is provided. So you still have to call it. While it could have been specified as optional if none of the superclasses had a non-default constructor, that would be adding complexity and making Derived's explicit constructor misleading (no call to super).

还有一些机械原因:在您调用 super 之前,未定义此,但您可以在调用 super <之前执行操作/ code>,因此在规范中处理的机制是必要的。

There are also some mechanical reasons: this isn't defined until you call super, but you're allowed to do things prior to calling super, so making the call is necessary to handle the mechanics of this in the spec.

这篇关于ES6 javascript类继承,为什么我们需要从派生类调用super()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆