我试图在我的 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/