什么是 JavaScript 速记属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38948306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is JavaScript shorthand property?
提问by exebook
var obj = { prop = [1,2,3] };
The code above contains a typo, there should be a colon instead of =
. But what surprised me was the VM error message:
上面的代码包含一个错字,应该有一个冒号而不是=
. 但令我惊讶的是 VM 错误消息:
var obj = { prop = [1,2,3] };
^^^^^^^^^^^^^^
SyntaxError: Invalid shorthand property initializer
I searched for "JavaScript shorthand properties" but this term is still not clear to me. What does "Shorthand property" means in context of this error message?
我搜索了“JavaScript 速记属性”,但我仍然不清楚这个术语。在此错误消息的上下文中,“简写属性”是什么意思?
回答by snak
With ES6, you can use shorthand property names which allow you to write something like this.
在 ES6 中,您可以使用速记属性名称来编写类似的内容。
var s = 'abc';
var n = 1;
var o = { s, n }; // This is equivalent to { s: s, n: n }
In your case, prop = [1,2,3]
was parsed as one shorthand property (s
and n
in the example above), but it was not a proper property name.
在你的情况下,prop = [1,2,3]
被解析为一个缩写属性(s
以及n
在上面的例子),但它不是一个正确的属性名称。
回答by Oriol
Firefox has a different error message, which in my opinion is more useful:
Firefox 有一个不同的错误信息,我认为它更有用:
SyntaxError: missing : after property id
That is, there is a missing :
. As you say, you should use :
instead of =
.
也就是说,缺少一个:
. 正如你所说,你应该使用:
而不是=
.
To make it clear, "shorthand property" has no meaning in the ES6 spec. It's just some expression Chrome invented to help you noticing your error. It seems they failed.
明确地说,“速记属性”在 ES6 规范中没有任何意义。这只是 Chrome 发明的一些表达方式,可以帮助您注意到错误。看来他们失败了。
snak's guessis that Chrome refers to a PropertyDefinitionconsisting of an IdentifierReference, used in an ObjectLiteral. Clearly prop = [1,2,3]
is not an IdentifierReference, so it might make sense to complain about it. It would make even more sense to complain that it's not a PropertyDefinitionin the much more common form of PropertyName:
AssignmentExpression. Or MethodDefinition.
snak 的猜测是 Chrome 指的是一个PropertyDefinition,它由一个IdentifierReference组成,在ObjectLiteral 中使用。显然prop = [1,2,3]
不是IdentifierReference,所以抱怨它可能是有意义的。它将使更多的意义,抱怨说这不是一个PropertyDefinition中的更常见的形式属性名:
AssignmentExpression。或MethodDefinition。
回答by Robert Rocha
Since there is no official explanation from MDN I can only assume what this might mean.
由于 MDN 没有官方解释,我只能假设这可能意味着什么。
Consider the following:
考虑以下:
There are two ways of creating an array.
创建数组有两种方法。
The long way (sort of):
漫长的道路(有点):
var cars = new Array("Saab", "Volvo", "BMW");
The short way:
简短的方法:
var cars = ["Saab", "Volvo", "BMW"];
The long and short way is more obvious when dealing with the creation of objects:
在处理对象的创建时,长短途更明显:
The long way:
路漫漫其修远兮:
var person = new Object();
person.firstName = "John";
person.lastName = "Doe";
The short way:
简短的方法:
var person = {firstName: "John", lastName: "Doe"};