简体   繁体   English

ES6 对象速记符号可以与常规对象符号结合使用吗?

[英]Can ES6 object shorthand notation be combined with regular object notation?

With ES6 we can now utilize object shorthand notation for creating objects...使用 ES6,我们现在可以使用对象速记符号来创建对象...

var a = 1, b = 2, c = 3;
var obj = { a, b, c };

Is it possible to combine shorthand notation with regular notation?是否可以将速记符号与常规符号结合使用?

In other words, is the following legit?换句话说,以下合法吗?

var obj = {a, b, c, d: 'foo'};

And if so, are there any gotchas I should be aware of?如果是这样,我应该注意哪些问题?

Is it possible to combine shorthand notation with regular notation?是否可以将速记符号与常规符号结合使用?

Yes.是的。 A property definition can be any of the following:属性定义可以是以下任何一种:

PropertyDefinition :
    IdentifierReference
    CoverInitializedName
    PropertyName : AssignmentExpression
    MethodDefinition

Source: ECMAScript 2015 Language Specification来源: ECMAScript 2015 语言规范

And if so, are there any gotchas I should be aware of?如果是这样,我应该注意哪些问题?

Nope.不。

According to Babel yes根据 Babel 是的

See transpiled code results 查看转译的代码结果

Babel translates this Babel翻译了这个

var a = 1, b = 2, c = 3;
var obj = {a, b, c, d: 'foo'};

into this in es5es5 中进入这个

var a = 1,
b = 2,
c = 3;

var obj = { a: a, b: b, c: c, d: 'foo' };

Also found a github repo by Luke Hoban that shows mixed objects being created还发现了 Luke Hoban 的github仓库,其中显示了正在创建的混合对象

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

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