最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论