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


  params.js:4
  costPerHr = {

          ^

  
  语法错误:意外令牌=


我的对象看起来像这样:

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,
        };
    }
};

10-06 15:05