Why does `{foo: 1}` evaluate to `1` in the console, and `{foo: 1, bar: 2}` results in an error?

前端 未结 4 662
闹比i
闹比i 2020-12-21 09:03

I knew that {} is either an object or a block of code, but today my co-worker asked me why {foo: 1} works when entered into the console, but

4条回答
  •  情歌与酒
    2020-12-21 09:39

    By itself, {a: 1} is a block statement, where a is a label.

    Of course, in a context where an expression is expected, it is an object literal:

    var o = { a: 1 };
    

提交回复
热议问题