This may be obvious, but I do not understand how to use module.export
to export both the subclass and superclass. I'm currently getting the error ReferenceError: not defined
. Here's the an example subclass Dalmatian
in /js/dalmatian.js
:
class Dalmatian extends Dog{
constructor(){
super();
/// stuff
}
}
module.exports = {
Dalmatian : Dalmatian
}
If I then export this class in another *.js
file, I run into problems:
require('../js/dog.js'); // this works
require('../js/dalmatian.js'); // this fails
ReferenceError: Dog is not defined
I don't understand. The super constructor is used within Dalmatian, i.e. super();
.
How do I export the base class (which here is Dog
) so I don't get this error?
This may be obvious, but I do not understand how to use module.export
to export both the subclass and superclass. I'm currently getting the error ReferenceError: not defined
. Here's the an example subclass Dalmatian
in /js/dalmatian.js
:
class Dalmatian extends Dog{
constructor(){
super();
/// stuff
}
}
module.exports = {
Dalmatian : Dalmatian
}
If I then export this class in another *.js
file, I run into problems:
require('../js/dog.js'); // this works
require('../js/dalmatian.js'); // this fails
ReferenceError: Dog is not defined
I don't understand. The super constructor is used within Dalmatian, i.e. super();
.
How do I export the base class (which here is Dog
) so I don't get this error?
- 1 you need to import the base Dog class from its module in the Dalmation before using it. Once exported, the modules can't see each other's methods/classes – Abid Hasan Commented Apr 22, 2018 at 4:37
- @AbidHasan Sorry, I don't follow. Could you make this more concrete with code? I'm still a beginner with Node.js – ShanZhengYang Commented Apr 22, 2018 at 4:43
2 Answers
Reset to default 5You have to require
the parent class in your child class declaration. Also export
the parent form subclass export
clause.
You can then use both Dog
and Dalmatian
from your script that requires('./dalmatian')
child class.
Here's a working example:
dog.js
class Dog{
constructor(){
console.log('dog');
}
}
module.exports = Dog;
dalmatian.js (note how we export both)
const Dog = require('./dog');
class Dalmatian extends Dog{
constructor(){
super();
console.log('dalmatian');
}
}
module.exports = {
Dalmatian : Dalmatian, //export this class
Dog: Dog // and export parent class too!
}
test.js
const Dalmatian = require('./dalmatian').Dalmatian;
const Dog = require('./dalmatian').Dog; //---> Notice this
//const Dog = require('./dog'); ---> works too, but above is clearer and cleaner
new Dalmatian();
new Dog();
Output:
➔ node test.js
dog
dalmatian
dog
Dog
is not defined in the module containing Dalmation
, because modules don't have access to each other's variables.
Your Dalmation module should look something like this:
var parentClass = require('./Dog.js')
class Dalmatian extends parentClass.Dog {
constructor(){
super();
console.log('starting dalmation')
}
}
module.exports = {
Dalmatian: Dalmatian
}
Also, note the super() should be called in the constructor method, not before it.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745651121a4638271.html
评论列表(0条)