2021-04-28 18:08:03 +02:00
|
|
|
There are situations where ``++super()++`` must be invoked and situations where ``++super()++`` cannot be invoked.
|
|
|
|
|
|
|
|
|
|
|
|
The basic rule is: a constructor in a non-derived class cannot invoke ``++super()++``; a constructor in a derived class must invoke ``++super()++``.
|
|
|
|
|
|
|
|
|
|
|
|
Furthermore:
|
|
|
|
|
|
|
|
* ``++super()++`` must be invoked before the ``++this++`` and ``++super++`` keywords can be used.
|
|
|
|
* ``++super()++`` must be invoked with the same number of arguments as the base class' constructor.
|
|
|
|
* ``++super()++`` can only be invoked in a constructor - not in any other method.
|
|
|
|
* ``++super()++`` cannot be invoked multiple times in the same constructor.
|
|
|
|
|
|
|
|
=== Known Limitations
|
|
|
|
|
|
|
|
* False negatives: some issues are not raised if the base class is not defined in the same file as the current class.
|
|
|
|
|
2020-12-21 15:38:52 +01:00
|
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
|
|
|
|
----
|
|
|
|
class Dog extends Animal {
|
|
|
|
constructor(name) {
|
|
|
|
super();
|
|
|
|
this.name = name;
|
|
|
|
super(); // Noncompliant
|
|
|
|
super.doSomething();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2020-12-21 15:38:52 +01:00
|
|
|
== Compliant Solution
|
|
|
|
|
|
|
|
----
|
|
|
|
class Dog extends Animal {
|
|
|
|
constructor(name) {
|
|
|
|
super();
|
|
|
|
this.name = name;
|
|
|
|
super.doSomething();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
2021-04-28 18:08:03 +02:00
|
|
|
|
|
|
|
|
2021-06-02 20:44:38 +02:00
|
|
|
|
|
|
|
ifdef::rspecator-view[]
|
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::comments-and-links.adoc[]
|
|
|
|
endif::rspecator-view[]
|