是否有 null
ES6(ES2015/JavaScript.next/Harmony)中的安全属性访问(空传播/存在)运算符,例如 ?.
在 CoffeeScript 中?还是计划用于 ES7?
var aThing = getSomething()
...
aThing = possiblyNull?.thing
这大致是这样的:
if (possiblyNull != null) aThing = possiblyNull.thing
Ideally the solution should not assign (even undefined
) to aThing
if possiblyNull
is null
原文由 נשמה קשוחה 发布,翻译遵循 CC BY-SA 4.0 许可协议
更新(2022-01-13):似乎人们仍在寻找这个,这是当前的故事:
更新(2017-08-01):如果你想使用官方插件,你可以尝试使用新转换的 Babel 7 alpha 构建。 你的旅费可能会改变
https://www.npmjs.com/package/babel-plugin-transform-optional-chaining
原文:
目前处于第 1 阶段的实现该功能的功能:可选链接。
https://github.com/tc39/proposal-optional-chaining
如果你现在想使用它,有一个 Babel 插件可以实现。
https://github.com/davidyaha/ecmascript-optionals-proposal