最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can ES6 object shorthand notation be combined with regular object notation? - Stack Overflow

programmeradmin0浏览0评论

With ES6 we can now utilize object shorthand notation for creating objects...

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

Is it possible to bine 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?

With ES6 we can now utilize object shorthand notation for creating objects...

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

Is it possible to bine 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?

Share Improve this question asked Mar 25, 2016 at 0:17 sfletchesfletche 49.9k31 gold badges109 silver badges120 bronze badges 1
  • 2 What was result when you tried ? – guest271314 Commented Mar 25, 2016 at 0:21
Add a ment  | 

2 Answers 2

Reset to default 11

Is it possible to bine 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

And if so, are there any gotchas I should be aware of?

Nope.

According to Babel yes

See transpiled code results

Babel translates this

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

into this in es5

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

发布评论

评论列表(0)

  1. 暂无评论