节点函数使用big.js返回数据类型



我是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">

?

似乎,您没有调用将返回函数定义的函数。如果您要返回数字,则toFixed将工作并将数字转换为字符串。

a.profit().toFixed(8) // invoke the function

最新更新