Optional chaining on the left side in Javascript

前端 未结 3 1751
悲&欢浪女
悲&欢浪女 2021-01-18 15:12

Is it possible to use the optional chaining operator in the left side of an assignment = in Javascript?

c         


        
相关标签:
3条回答
  • 2021-01-18 16:00

    It's not possible, sorry.

    In the interest of a canonical answer: The MDN documentation isn't explicit about this, but you can read the proposal's README in GitHub for more information. It says:

    The following is not supported, although it has some use cases; see Issue #18 for discussion:

    • optional property assignment: a?.b = c

    In the linked issue are the comments 1:

    OK, seems like there's a rough agreement in this thread not to do the write case in the first iteration.

    and 2:

    We also discussed this question at TC39, and the committee did not seem so interested in adding this feature.

    So I guess it's not likely to happen anytime soon.

    Hope that helps; good luck!

    0 讨论(0)
  • No, it is not possible. The documentation clearly states

    The optional chaining operator ?. permits reading the value of a property located deep within a chain of connected objects without having to expressly validate that each reference in the chain is valid.

    0 讨论(0)
  • 2021-01-18 16:02

    I’ve tried this on Chrome Canary (because of the browser compatibility) and it didn’t work:

    0 讨论(0)
提交回复
热议问题