admin管理员组文章数量:1026630
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.
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.
本文标签:
版权声明:本文标题:javascript - How to to export both the class and superclass in Node.js using module.exports and require()? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745651119a2161325.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论