javascript - My class created doesn't export an object
问题描述
I need to create the class ShoppingCart in the file ShoppingCart.js and export it to a test file and i get the error that my class is not a constructor
I know the problem is not in import export because before creating the js file i got the error that it couldn't find the module. I also tried creating a new instance of the class inside the file and it worked
file ShoppingCart.js
class ShoppingCart{
constructor(name){
this.name=name
}
}
module.exports = { ShoppingCart}
The code for my test file is
const ShoppingCart = require("./ShoppingCart")
new ShoppingCart()
when i run the test file i get
TypeError: ShoppingCart is not a constructor
解决方案
您当前正在导出具有以下属性的对象ShoppingCart
:
module.exports = { ShoppingCart }
// ^^ object ^^
只需导出ShoppingCart
:
module.exports = ShoppingCart;
或者,在导入时,引用ShoppingCart
对象的属性:
const { ShoppingCart } = require("./ShoppingCart")
推荐阅读
- java - 使用自定义登录时从 Firebase 中获取重复项
- python - 如何在不滞后程序的情况下监控新/删除的串行设备连接?
- python - 我怎样才能让我的功能不断要求用户输入,直到它是正确的?
- python - ModuleNotFoundError:使用从 tensorflow.contrib.seq2seq 导入帮助器时没有名为“tensorflow.contrib”的模块
- python - Python cx_Oracle XMLTYPE 数据检索错误 DPI-1040: LOB 已关闭
- directx-11 - 如何在 D3D11 中获取资源分配大小
- django - ec2上的Django celery worker错误尝试使用systemd启动守护进程
- vba - 将 PPTX 文件另存为 JPEG 时如何知道 JPEG 文件的名称?
- java - 以 10 为底的整数公式补码的证明在哪里?
- swift - 如何在 Swift 中将 RGB 十六进制值转换为 UIColor?