Reputation: 105547
Here is the code:
class Foo {
static m() {
console.log('m');
}
}
class Bar extends Foo {
b() {
super.m();
}
}
var b = new Bar();
b.b();
that throws:
Uncaught TypeError: (intermediate value).m is not a function
This type of error is generated when I attempt to use parent class static methods in an instance method or a constructor. Why?
As I understand, super
refers to the Foo
, and the following works OK:
class Bar extends Foo {
b() {
Foo.m();
}
}
Upvotes: 0
Views: 104
Reputation: 223164
Because super
properties refer to Foo.prototype
properties in instance methods, and super
properties refer to Foo
static properties in static methods.
super(...)
call in constructor has special meaning and does something like Foo.call(this, ...)
internally.
super
alone has no meaning and cannot be used as a regular variable.
It should be:
b() {
this.constructor.m();
}
because Bar
inherits m
static method from Foo
.
Or if it specifically should be parent class,
b() {
super.constructor.m();
}
Upvotes: 4
Reputation: 569
Try to examine an instance of Foo
.
You will see that m
is not a regular member of the Foo
instance but it is "hooked" under the constructor
as a public static method.
For you code to work, just change the b
method definition on Bar class
to:
b() {
super.constructor.m();
}
More on this topic can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static
Upvotes: 0