【发布时间】:2019-10-10 04:09:31
【问题描述】:
我的代码在这部分失败了..我无法理解这个 && 运算符在 js 中的作用是什么?
export function calculateLineTotals(line, invoice) {
let totals = _calculateLineTotals(line, invoice)
return {
amount: totals.amount &&
totals.amount.decimalPlaces(2).toNumber(),
vatRate: totals.vatRate &&
totals.vatRate.decimalPlaces(2).toNumber(),
grossAmount: totals.grossAmount &&
totals.grossAmount.decimalPlaces(2).toNumber(),
vatAmount: totals.vatAmount &&
totals.vatAmount.decimalPlaces(2).toNumber(),
}
【问题讨论】:
-
阅读有关 logical operators 的 JavaScript 文档。
-
“我的代码在这里失败了” - 你能更具体地说明它是如何失败的吗?您的浏览器控制台中有错误消息吗?
-
你可以读成这样:
if totals.amount is truthy,那么我希望它得到totals.amount.decimalPlaces(2).toNumber(),否则它将是undefined
标签: javascript