I have a file with the following code:
class Animal {
doSomething = () => {
return 'Hi.';
};
}
class Dog extends Animal {
doSomething = () => {
return super.doSomething() + ' Woof!';
};
}
console.log(new Dog().doSomething());
Note: trying to run the snippet above probably won't work because I can't figure out how to make it use my Babal settings.
Anyway, when I compile it using Babel and run it in Node, I get the following error:
/Users/elias/src/classFieldTest/build/classFieldTest.js:15
return super.doSomething() + ' Woof!';
^
TypeError: (intermediate value).doSomething is not a function
at Dog.doSomething (/Users/elias/src/classFieldTest/build/classFieldTest.js:15:26)
at Object.<anonymous> (/Users/elias/src/classFieldTest/build/classFieldTest.js:21:23)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
I am using Babel 6.26.0 with the stage-2
preset, and Node 8.11.1. I can show the commands I'm using if anyone cares.
Why is this happening? I am guessing that super
can't be used to access a class field, but what am I supposed to do about this? If I change the doSomething
method of Animal
to a traditional method (doSomething() { return 'Hi.'; }
), it works, but I would rather avoid traditional methods, with the way they redefine this
and all the confusion it causes.
Is there any way to access a class field of a superclass?