What does the colon mean in this JavaScript snippet (not an object literal)?

后端 未结 1 1342
隐瞒了意图╮
隐瞒了意图╮ 2021-01-25 13:25

I apologize for posting a duplicate-looking question, (I know, that there is a bunch of similar titled questions here), but none of the questions already present seems to suit m

相关标签:
1条回答
  • 2021-01-25 14:06

    If you'd have read further down the page you linked, you would see why it was written like that.

    var func = () => { foo: 1 };
    

    This is an attempt to return an object from a arrow function.

    That doesn't work for reasons explained here:

    This is because the code inside braces ({}) is parsed as a sequence of statements (i.e. foo is treated like a label, not a key in an object literal). (source)

    So the returned value needs to be wrapped in parentheses:

    var func = () => ({foo: 1});
    

    To actually answer your question:

    It's a label.

    You can't just take the foo: 1 out of context like that.

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