This question already has an answer here:
What does the colon mean in this JavaScript snippet (not an object literal)?

(1个答案)


3年前关闭。




我的一位同事编写了ES6代码行...
return map(orderedContentUuids, contentUuid => { uuid: contentUuid });
您可能会猜到他打算返回对象{uuid: contentUuid },但是由于其带有箭头功能,花括号{实际上开始了一个新块。 (正确的代码应为return map(orderedContentUuids, contentUuid => ({ uuid: contentUuid }));)。

但是,出乎意料的是,此代码转码并运行时没有错误。没有错误,因为uuid: contentUuid似乎评估为contentUuid

然后,您可以看到,如果将其放入JavaScript控制台foo: 'bar',则其评估结果为"bar"

??这是怎么回事。从什么时候开始有效的JS?

最佳答案

哎呀我只是想通了。
foo: 'bar'被评估为“标签”,但我没有意识到这是JavaScript功能。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label

09-25 20:00