Javascript 左侧的可选链接 [英] Optional chaining on the left side in Javascript

查看:38
本文介绍了Javascript 左侧的可选链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用可选链接= 左侧的 a> 运算符?

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

const building = {}
building?.floor?.apartment?.number = 3; // Is this possible?

推荐答案

这不可能,抱歉.

为了规范答案:MDN 文档对此没有明确说明,但您可以阅读 GitHub 中的提案自述文件 了解更多信息.它:

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:

不支持以下内容,尽管它有一些用例;请参阅问题 #18 进行讨论:

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

  • 可选的属性赋值:a?.b = c

在链接的问题中是评论1:

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.

2:

我们也在 TC39 上讨论过这个问题,委员会似乎对添加这个功能不太感兴趣.

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!

这篇关于Javascript 左侧的可选链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆