javascript - How to to export both the class and superclass in Node.js using module.exports and require()? - Stack Overflow

This may be obvious, but I do not understand how to use module.export to export both the subclass and s

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?

Share Improve this question edited Apr 22, 2018 at 5:23 Shaunak 18.1k5 gold badges56 silver badges86 bronze badges asked Apr 22, 2018 at 4:34 ShanZhengYangShanZhengYang 17.7k52 gold badges142 silver badges248 bronze badges 2
  • 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
Add a ment  | 

2 Answers 2

Reset to default 5

You 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信