且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JavaScript中的空值合并运算符

更新时间:2022-06-14 23:15:44

现在您必须这样做:

(this.state.updateItem || {}).unit || 'Unit'

ES(JavaScript)针对可选链接的第一阶段提案我们(最终,希望如此)将能够执行以下操作:

There is a stage one proposal to ES (JavaScript) for optional chaining whereby we'll (eventually, hopefully) be able do do something like this:

this.state.updateItem?.unit || 'Unit'

如果您正在做通天塔,您现在就可以使用它!:
https://www.npmjs.com/package/babel-plugin-转换可选链

And if you're doing babel you can use it now!:
https://www.npmjs.com/package/babel-plugin-transform-optional-chaining

该提案现在处于第4阶段(截至2020年1月),并将被添加到JavaScript标准中

The proposal is now in stage 4 (as of January 2020) and will be added into the JavaScript standard