为什么`{foo:1}`在控制台中评估为“1”,而`{foo:1,bar:2}`会导致错误? [英] Why does `{foo: 1}` evaluate to `1` in the console, and `{foo: 1, bar: 2}` results in an error?

查看:137
本文介绍了为什么`{foo:1}`在控制台中评估为“1”,而`{foo:1,bar:2}`会导致错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 {} 是一个对象或一块代码,但今天我的同事问我为什么 {foo:1 } 在输入控制台时有效,但 {foo:1,bar:2} 会产生错误。

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 {foo: 1, bar: 2} generates an error.

为什么 foo:1 在控制台中评估为 1

Why does foo: 1 evaluate in the console to 1?

推荐答案

{a:1} 本身是一个块语句,其中 a 是一个标签。

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 };

这篇关于为什么`{foo:1}`在控制台中评估为“1”,而`{foo:1,bar:2}`会导致错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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