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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章