javascript - 类里面的字典?

标签 javascript node.js class ecmascript-6

我试图在我的 javascript 类中创建字典/对象并通过 nodejs 运行它,但我收到此错误:

params.js:4
costPerHr = {

          ^

SyntaxError: Unexpected token =

我的对象看起来像这样:

module.exports = class Params {
  constructor() {}

  costPerHr = {
    internal: 100,
    shore: 50,
  };

  hoursPerMonth = 160;

  swhwMultiplier = {
    2: 280 / hoursPerMonth,
    5: 470 / hoursPerMonth,
  };
};

最佳答案

要在类中设置属性,您必须在构造函数中设置它们:

module.exports = class Params {
    constructor() {
        this.costPerHr = {
            internal: 100,
            shore: 50,
        };

        this.hoursPerMonth = 160;

        this.swhwMultiplier = {
            2: 280 / this.hoursPerMonth,
            5: 470 / this.hoursPerMonth,
        };
    }
};

关于javascript - 类里面的字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54363406/

相关文章:

javascript - 在 HTML5 中从 sql 检索数据

javascript - 从点击事件处理程序中排除元素 (jquery)

javascript - slickgrid 标题的 Colspan 和 rowspan

javascript - Nodejs 和 Angular 应用程序使用 JWT 发送 401 Unauthorized 和 200 OK

javascript - 如何在从根遍历到json树的目标 Node 时插入键?

JavaScript fetch() Discord 文件上传导致 HTTP 400 "cannot send empty message, 50006"错误

node.js - FS Node 删除除名称匹配的所有文件

class - Swift 中结构体和类的区别

jquery - 选择末尾具有唯一编号的属性

python - 让所有属性出现在 python 的 `__dict__` 方法上