Null-safe property access (and conditional assignment) in ES6/2015

后端 未结 10 1149
[愿得一人]
[愿得一人] 2020-11-22 13:00

Is there a null-safe property access (null propagation / existence) operator in ES6 (ES2015/JavaScript.next/Harmony) like ?. in

10条回答
  •  -上瘾入骨i
    2020-11-22 13:59

    Update (2020-01-31): Seems people are still finding this, here's the current story:

    • Optional Chaining specification (Stage 4): https://github.com/tc39/proposal-optional-chaining
    • babel-preset-env: This is probably what you want https://babeljs.io/docs/en/babel-preset-env
    • Babel v7 Plugin: https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining

    Update (2017-08-01): If you want to use an official plugin, you can try the alpha build of Babel 7 with the new transform. Your mileage may vary

    https://www.npmjs.com/package/babel-plugin-transform-optional-chaining

    Original:

    A feature that accomplishes that is currently in stage 1: Optional Chaining.

    https://github.com/tc39/proposal-optional-chaining

    If you want to use it today, there is a Babel plugin that accomplishes that.

    https://github.com/davidyaha/ecmascript-optionals-proposal

提交回复
热议问题