75 lines
2.2 KiB
Plaintext
75 lines
2.2 KiB
Plaintext
== Why is this an issue?
|
|
|
|
In JavaScript, the `super` keyword is used to call the constructor and methods of an object's parent class, and to access its properties.
|
|
|
|
The expression ``++super(...args)++`` is used to call the parent's constructor. It must be used carefully and correctly to avoid errors.
|
|
|
|
[source,javascript,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
class Dog extends Animal {
|
|
constructor(name) {
|
|
super();
|
|
this.name = name;
|
|
super(); // Noncompliant: constructor is called twice.
|
|
super.doSomething();
|
|
}
|
|
}
|
|
----
|
|
|
|
Follow these instructions when invoking the parent's constructor:
|
|
|
|
* ``++super()++`` cannot be invoked in the constructor of a non-derived class.
|
|
* ``++super()++`` must be invoked in the constructor of a derived class.
|
|
* ``++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.
|
|
|
|
[source,javascript,diff-id=1,diff-type=compliant]
|
|
----
|
|
class Dog extends Animal {
|
|
constructor(name) {
|
|
super();
|
|
this.name = name;
|
|
super.doSomething();
|
|
}
|
|
}
|
|
----
|
|
|
|
Some issues are not raised if the base class is not defined in the same file as the current class. This is a known limitation of this rule.
|
|
|
|
== Resources
|
|
=== Documentation
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super[`super`]
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes[Classes]
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain[Inheritance and the prototype chain]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Lacked a call of 'super()' in some code paths.
|
|
|
|
Expected to call 'super()'.
|
|
|
|
Unexpected duplicate 'super()'.
|
|
|
|
Unexpected 'super()' because 'super' is not a constructor.
|
|
|
|
Unexpected 'super()'.
|
|
|
|
'super'|'this' is not allowed before 'super()'.
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== supercedes: S3833
|
|
|
|
endif::env-github,rspecator-view[]
|