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

相关推荐

  • 计算机开机长鸣报警,电脑开机报警,详细教您电脑开机一直长鸣报警怎么办

    朋友们在使用电脑的过程中,经常会碰到开机一声长鸣,然后一直响个不停,这到底是怎么回事呢? 其实这个现象的产生一般是因为内存的问题,那用户遇到电脑开机一直

    1小时前
    00
  • UMIT:统一多模态多任务视觉

    随着深度学习的迅速发展,尤其是在医学影像分析领域的应用,越来越多的视觉-语言模型(VLMs)被广泛应用于解决复杂的健康和生物医学挑战。然而,现有研究主要集中在特定任务或单一模态上,这限制了它们在多种医学场景中的适用性和泛化能力。为了解决这

    1小时前
    00
  • PyMC+AI提示词贝叶斯项目反应IRT理论Rasch分析篮球比赛官方数据:球员能力与位置层级结构研究

    全文链接:tecdat?p=41666在体育数据分析领域不断发展的当下,数据科学家们致力于挖掘数据背后的深层价值,为各行业提供更具洞察力的决策依据。近期,我们团队完成了一项极具意义的咨询项目,旨在通过先进的数据分析手段,深入探究篮球比赛中

    1小时前
    00
  • 万字图解 Java 并发框架:ForkJoin、CountDownLatch、Semaphore、CyclicBarrier

    本文图多,内容硬核,建议收藏。在第一章节《1.6w 字图解 Java 并发:多线程挑战、线程状态和通信、死锁;AQS、ReentrantLock、Condition 使用和原理》,我们开启了 Java 高并发系列的学习,透彻理解 Java

    1小时前
    00
  • 1.8w字图解Java并发容器: CHM、ConcurrentLinkedQueue、7 种阻塞队列的使用场景和原理

    文章多图且内容硬核,建议大家收藏上一章《1.6w 字图解 Java 并发:多线程挑战、线程状态和通信、死锁;AQS、ReentrantLock、Condition 使用和原理》,我们开启了 Java 高并发系列的学习,透彻理解 Java 并

    1小时前
    00
  • 初始JESD204B高速接口协议(JESD204B一)

    01、对比LVDS与JESD204JESD204B是逻辑器件和高速ADCDAC通信的一个串行接口协议,在此之前,ADCDAC与逻辑器件交互的接口大致分为如下几种。低速串行接口(I2C、SPI)、低速并行接口(包含时钟信号和并行数据信号,

    1小时前
    00
  • 我用AI监控了奥特曼,当他一发推特AI就会自动给我打电话。

    上周我真的扛不住了。奥特曼这个孙贼,发了个X说,“要发一个礼拜的好东西”。我信了他的邪,明明出差1周,每天早上9点不到就要起来参加活动,但是晚上根本不敢睡觉,天天蹲到凌晨3点半,蹲到他们那边时间中午12点多,我才敢去睡觉。真的,那一整周,我

    59分钟前
    00
  • 20 万 POC,直接拿来用,这不是测试,这是拒绝服务!!!

    之前看到很多人分享 github 上的一个项目,自动收录全网 nuclei 模板文件,目前已经 19 万了,如果直接拿来对目标进行漏洞探测,无疑会对目标造成巨大伤害,意味着可能要对目标发起十九万次请求以上,可以说是一次小型的 DDoS 攻击

    51分钟前
    00
  • Go 语言 Mock 实践

    Mock 是软件测试中的一项关键技术,尤其在单元测试领域,可谓是“顶梁柱”般的存在,几乎不可或缺。它通过模拟真实对象的行为,使我们能在不依赖外部系统的情况下,专注测试代码的核心逻辑。对于测试开发、自动化测试,乃至性能测试中的某些场景,合理使

    35分钟前
    00
  • CUT&Tag 数据处理和分析教程(7)

    过滤某些项目可能需要对比对质量分数进行更严格的过滤。本文细讨论了bowtie如何分配质量分数,并举例说明。MAPQ(x) = -10 * log10log10(P(x is mapped wrongly)) = -10 * log10(p)

    30分钟前
    10
  • 推荐一个轻量级的监控平台并且支持移动端

    简介XUGOU 是基于Cloudflare构建的轻量化监控平台,专精于系统资源监控与可视化状态页面服务。该平台提供英文简体中文双语支持,满足全球化部署需求。面向开发者及中小团队,项目致力于提供高可用性的监控解决方案。核心功能与实现平台功能

    24分钟前
    00
  • maxwell遇到的一则问题

    结论和原因maxwell的元数据库里面没有存储全部的schema数据(就是少数据了),导致相关表的DDL校验失败。PS:我这里maxwell的作用只是采集库表修改情况的统计粗粒度指标,因为之前maxwell在运行报错的时候,直接修改了pos

    16分钟前
    00
  • Windows Server20192022 Evaluation评估版未激活导致关机问题

    摘要:在安装Windows Server 20192022后,会出现系统版本为 Evaluation 评估版情况,如提示Windows许可证已到期,就

    14分钟前
    00
  • Nat. Mater.

    大家好,今天给大家分享一篇近期发表在Nat. Mater.上的研究进展,题为:De novo design of self-assembling peptides with antimicrobial activity guided

    11分钟前
    00
  • 人工智能适合什么人学

    一、引言:人工智能浪潮下的新机遇在当今科技飞速发展的时代,人工智能(AI)无疑是最为耀眼的技术明星之一。从智能语音助手到自动驾驶汽车,从医疗诊断辅助到金融风险预测,人工智能正以前所未有的速度改变着我们的生活和工作方式。随着全球领先的终身学习

    8分钟前
    00
  • 子网掩码是怎么“掩”的?用积木教你彻底搞懂!

    子网掩码是怎么“掩”的?用积木教你彻底搞懂!前言肝文不易,点个免费的赞和关注,有错误的地方请指出,看个人主页有惊喜。作者:神的孩子都在歌唱你是不是也曾被“子网掩码”这个术语搞得晕头转向?明明是学网络的第一步,却像是打开了数学世界的大门:2

    7分钟前
    00
  • 雨晨 26200.5516 Windows 11 IoT 企业版 LTSC 2024 轻装版

    简述:以下为YCDISM (雨晨作品自2025年03月25日起通用介绍,若无重大更改不再额外敖述) 全程由最新YCDISM2025脱机装载26100.1742_zh-cn_windows_11_

    4分钟前
    00
  • ​2025 轻松部署 Odoo 18 社区版

    随着 Odoo 18 社区版的发布,越来越多的企业希望借助这款开源 ERP 系统实现数字化转型。本文将深入解析传统部署方式的底层逻辑,并揭示如何通过自动化工具实现零门槛快速部署。一、手工部署 Odoo 18 技术全解 Docker 环境搭建

    3分钟前
    00
  • 用Xshell8配置密钥登陆

    1.首先在服务端查看root.sshauthorized_keys是否存在,这是存储公钥的文件,若不存在需新建此文件 2. 打开Xshell8,选择"新建",选择"新建用户密钥生成向导" 给用户

    54秒前
    00
  • 人工智能应用领域有哪些

    人工智能应用领域有哪些一、引言随着科技的飞速发展,人工智能(AI)已经逐渐渗透到我们生活的方方面面,成为推动社会进步的重要力量。从医疗健康到金融服务,从教育学习到智能制造,人工智能以其独特的技术优势,为各行各业带来了前所未有的变革。本文旨在

    37秒前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信