我是node的新手,正在尝试创建一个具有三个属性的类,其中一个属性是计算出来的。
const Big = require("big.js"); // https://www.npmjs.com/package/big-js
class Trade{
constructor(name, buy, sell) {
this.name = name;
this.buy = Big(buy);
this.sell = Big(sell);
}
profit() {
return this.sell.minus(this.buy);
}
};这是因为我可以使用以下命令创建一个新对象:
a = new Trade("Widget", 123.12345678, 321.87654321)
a.buy.toFixed(8) // 123.12345678
a.sell.toFixed(8) // 321.87654321但是我不能这样做:
a.profit.toFixed(8)
Uncaught TypeError: a.profit.toFixed is not a function我如何在我的类中创建一个计算属性,并让它返回一个"Big.js“数据类型,以便我可以使用"Big.js”库中包含的方法,比如"toFixed“?
发布于 2021-09-05 12:37:00
看起来,您没有调用将返回函数定义的函数。如果您要返回数字,toFixed将工作并将数字转换为字符串。
a.profit().toFixed(8) // invoke the functionhttps://stackoverflow.com/questions/69063341
复制相似问题