Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
123
rated 0 times [  130] [ 7]  / answers: 1 / hits: 34314  / 8 Years ago, mon, august 15, 2016, 12:00:00
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:



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?


More From » javascript

 Answers
7

With ES6, you can use shorthand property names which allow you to write something like this.



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.


[#61038] Thursday, August 11, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sylviakaitlinj

Total Points: 564
Total Questions: 114
Total Answers: 105

Location: Jordan
Member since Thu, Aug 11, 2022
2 Years ago
;