That's because you can call a parent method, but not a parent instance member.
The reason for that is proper methods are saved to the prototype chain, whereas member functions are not.
class Foo {
foo() {
return `Successfully called Foo.foo()`;
}
bar = () => {
return `Successfully called Foo.bar()`;
}
}
console.log(
('foo' in Foo.prototype), // true (methods are correctly inherited)
('bar' in Foo.prototype), // false (instance properties are not inherited)
);
If the property is not in the property chain, an attempt to call it by using super
will cause a runtime error.
class Bar extends Foo {
foo = () => {
return super.foo(); // Good: `foo` is in the prototype chain
}
bar = () => {
return super.bar(); // Runtime error: `bar` is not in the prototype chain
}
}
This makes it safe to go from a method to class instance property (here: foo
), but not the other way around.