ES6/2015 中的空安全属性访问(和条件赋值) - Null-safe property access (and conditional assignment) in ES6/2015

问题:

Is there a null -safe property access (null propagation / existence) operator in ES6 (ES2015/JavaScript.next/Harmony) like ?.在 ES6 (ES2015/JavaScript.next/Harmony) 中是否有一个null属性访问(null 传播/存在)运算符,比如?. in CoffeeScript for example?例如在CoffeeScript中? Or is it planned for ES7?还是计划用于 ES7?

var aThing = getSomething()
...
aThing = possiblyNull?.thing

This will be roughly like:这将大致如下:

if (possiblyNull != null) aThing = possiblyNull.thing

Ideally the solution should not assign (even undefined ) to aThing if possiblyNull is null理想情况下,解决方案不应该分配(甚至undefined ),以aThing如果possiblyNullnull


解决方案:

参考一: https://en.stackoom.com/question/2AqqE
参考二: https://stackoom.com/question/2AqqE
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章