[英]What is the : operator in ECMAScript/JavaScript?
So an object like 所以像
{bar: 2}
will return the object itself, which is 将返回对象本身,即
{bar: 2}
But if I try 但是如果我尝试
foo: {bar: 2}
Then I will get 2
instead 那我拿
2
What is the : operator does here exactly? :运算符在这里到底是什么? I can't find it in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators ...
我在https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators中找不到它...
First of all, absent other context: 首先,没有其他上下文:
{bar: 2}
does not create an object. 不创建对象。 It's a block statement with a single statement inside, and that statement has a label:
这是一个块语句,内部只有一个语句,并且该语句带有标签:
{
bar: // the label
2 // the expression statement
}
The text {bar: 2}
is only an object when it appears in the context of an expression , like this: 文本
{bar: 2}
仅当出现在表达式的上下文中时才是对象,如下所示:
foo({bar: 2}); // a function call to "foo", passing an object
Otherwise, a statement that begins with {
is always interpreted as a block. 否则,以
{
开头的语句始终被解释为块。
The statement 该声明
foo: {bar: 2}
is basically the same. 基本上是一样的 Here, there's a label (
foo:
) outside the block, and a label ( bar:
) inside. 这里,在块外有一个标签(
foo:
,在里面有一个标签( bar:
:)。
When you're trying code via a browser console, or Node, things get weird because the console itself has behavior intended to help with debugging but which is also different from language semantics in some cases. 当您通过浏览器控制台或Node尝试代码时,事情变得很奇怪,因为控制台本身具有旨在帮助调试的行为,但是在某些情况下也与语言语义有所不同 。 JavaScript is not directly a "REPL-friendly" syntax, so the console mechanism has to be clever.
JavaScript不直接是“ REPL友好”语法,因此控制台机制必须很聪明。
JavaScript doesn't have a :
operator. JavaScript没有
:
运算符。 But :
is used in various syntax productions. 但是
:
用于各种语法。
Within an object initializer's property initializer, the :
separates the property name from the property value . 在对象初始值设定项的属性初始值设定项中,
:
将属性名与属性值分开。 That's what you're using in your first example. 这就是您在第一个示例中使用的。
At statement level, not in an object initializer, a name followed by :
is a statement label . 在语句级别(而不是在对象初始化程序中),名称后跟
:
是语句标签 。 That's what your foo: {bar: 2}
example is. 这就是您的
foo: {bar: 2}
示例。 Labels are mostly used for directed break
and continue
. 标签主要用于定向
break
和continue
。
( :
is also used in the conditional operator , condition ? a :b
.) (
:
也用于条件运算符 condition ? a :b
。)
So then the question is: Why do you see {bar: 2}
in the console in the first example, and 2
in the console in the second example? 那么接下来的问题是:为什么你看到
{bar: 2}
在第一个例子中的控制台, 2
在第二个例子中,控制台?
The answer is that it's special behavior of the console, in two different ways: 答案是它是控制台的特殊行为,有两种不同的方式:
When you type {bar: 2}
into the console, it treats that as an expression, and so it sees it as an object initializer and creates, then logs, the object. 当您在控制台中输入
{bar: 2}
,它会将其视为一个表达式,因此将其视为对象初始化器,然后创建该对象并进行记录。 But if you had {bar: 2}
at the top level of script code, it wouldn't be an expression, it would be a block with a labelled statement in it. 但是,如果您在脚本代码的顶层具有
{bar: 2}
,则它不是表达式,而是带有标签语句的块。 Treating it as an expression is console-specific. 将其视为表达式是特定于控制台的。
When you type foo: {bar: 2}
into the console, it correctly identifies that a labelled statement — two of them, in fact: foo: ...
and bar: 2
. 当您在控制台中输入
foo: {bar: 2}
,它会正确地识别出一个带标签的语句-实际上是其中的两个: foo: ...
和bar: 2
。 The {}
are a block, not an object initializer, and so the inner bar:
is a label. {}
是一个块,而不是对象初始化器,因此内部的bar:
是一个标签。
So why 2
then? 那么为什么
2
? Because it's showing you the result of that code, which isn't available to us syntactically, but is defined by the specification. 因为它向您显示了该代码的结果,这在语法上对我们而言不可用,但由规范定义。 The result of a block is the last value evaluated within that block, and the labels are transparent.
块的结果是该块中最后求值的值,并且标签是透明的。 The last value in the
{bar: 2}
block is 2
, so the block's value is 2
, so that's what it shows. {bar: 2}
块中的最后一个值为2
,因此该块的值为2
,这就是显示的内容。 But again, this is not something we can directly observe in code, it's console behavior. 但是同样,这不是我们可以直接在代码中观察到的东西,它是控制台行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.