简体   繁体   English

没有大括号的Javascript键/值对

[英]Javascript key/value pair with no braces

I noticed that in both nodejs and a browser, you can give a key/value pair with no braces, and it is accepted. 我注意到在nodejs和浏览器中,你可以给出没有大括号的键/值对,并且它被接受。 It evaluates to the value half of the pair. 它评估该对的一半值。 For instance: 例如:

> { id: 5 }
{ id: 5 }
> id: 5
5

But if you quote id , then node wants more input: 但如果你引用id ,那么节点需要更多输入:

> "id": 5
... 

So what is going on here? 那么这里发生了什么? What syntax is id: 5 when it has no braces? id: 5是什么语法id: 5没有括号时?

This is not a bug. 这不是一个错误。 You are declaring a label, followed by a Number literal. 您正在声明一个标签,后跟一个Number文字。

The console defaults to printing out the result of the last expression in your code. 控制台默认打印出代码中最后一个表达式的结果。

Hence it prints 5 . 因此它打印5

Your confusion stems from the fact that the exact same syntax can mean totally different things, depending on the context . 您的困惑源于这样一个事实: 完全相同的语法可能意味着完全不同的东西,具体取决于上下文

This is for the JavaScript parser to decide according to the rules of the spec. 这是JavaScript解析器根据规范的规则来决定的。

Another example would be: 另一个例子是:

{}

Is that an object literal or a block? 这是一个对象文字还是块? The context provides the answer. 上下文提供了答案。

From the spec : 规格

A Statement may be prefixed by a label. 声明可以以标签为前缀。 Labelled statements are only used in conjunction with labelled break and continue statements. 带标签的语句仅与带标签的break和continue语句一起使用。 ECMAScript has no goto statement. ECMAScript没有goto语句。 A Statement can be part of a LabelledStatement, which itself can be part of a LabelledStatement, and so on. Statement可以是LabelledStatement的一部分,LabelledStatement本身可以是LabelledStatement的一部分,依此类推。 The labels introduced this way are collectively referred to as the “current label set” when describing the semantics of individual statements. 在描述各个语句的语义时,以这种方式引入的标签统称为“当前标签集”。

Imagine you've written a nested for..loop . 想象一下,你已经编写了一个嵌套的for..loop

for(var x = 0; x < 10; x++) {
  for(var y = 0; y < 10; y++) {
    if(thereIsAProblem) {
      break;
    }
  }
  console.log('done a column');
}

You want to break out of the inner loop and start the next iteration of the outer loop, but you don't want the console.log to run. 您想要打破内部循环并开始外部循环的下一次迭代,但您不希望console.log运行。

You can augment the loop with a label to specify exactly where you want to continue from. 您可以使用标签扩充循环,以准确指定要继续的位置。

outer: for(var x = 0; x < 10; x++) {
  for(var y = 0; y < 10; y++) {
    if(thereIsAProblem) {
      continue outer;
    }
  }
  console.log('done a column');
}

Although technically this is a standard feature in JavaScript, you won't see it used in the wild very often, because there's almost always a more idiomatic way to do things. 虽然从技术上讲这是JavaScript中的一个标准功能,但是你不会经常看到它在野外使用,因为几乎总是有更惯用的方式来做事。

I think it's a bug of the JavaScript parser. 我认为这是JavaScript解析器的一个错误。 When you type id : anything : 5 it outputs the same result (this works on Chrome too). 当您输入id : anything : 5它会输出相同的结果(这也适用于Chrome)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM