admin管理员组文章数量:1323715
The question I have is best given by way of this jsfiddle, the code for which is below:
var a = 1, b = 'x', c = true;
var d = {a: a, b: b, c: c}; // <--- object literal
var e = [a, b, c]; // <--- array
var f = {a, b, c}; // <--- what exactly is this??
// these all give the same output:
alert(d.a + ', ' + d.b + ', ' + d.c );
alert(e[0] + ', ' + e[1] + ', ' + e[2]);
alert(f.a + ', ' + f.b + ', ' + f.c );
What sort of a data structure is f
? Is it just a shorthand for d
?
The question I have is best given by way of this jsfiddle, the code for which is below:
var a = 1, b = 'x', c = true;
var d = {a: a, b: b, c: c}; // <--- object literal
var e = [a, b, c]; // <--- array
var f = {a, b, c}; // <--- what exactly is this??
// these all give the same output:
alert(d.a + ', ' + d.b + ', ' + d.c );
alert(e[0] + ', ' + e[1] + ', ' + e[2]);
alert(f.a + ', ' + f.b + ', ' + f.c );
What sort of a data structure is f
? Is it just a shorthand for d
?
3 Answers
Reset to default 80var f = {a, b, c};
It came with ES6 (ECMAScript 2015) and means exactly the same as:
var f = {a: a, b: b, c: c};
It is called Object Literal Property Value Shorthands (or simply property value shorthand, shorthand properties).
You can also combine shorthands with classical initialization:
var f = {a: 1, b, c};
For more information see Property definitions in Object initializer.
It is an Object Initializer Property Shorthand in ES6.
var f = {a, b, c, d:1}; // Will be equal to {a:a, b:b, c:c, d:1}
This works because the property value has the same name as the property identifier. This a new addition to the syntax of Object Initialiser (section 11.1.5) in the latest ECMAScript 6 draft Rev 13. And of course, just like the limitations set from ECMAScript 3, you can’t use a reserved word as your property name.
Such a shorthand won’t dramatically change your code, it only makes everything a little bit sweeter!
function createCar(name, brand, speed) {
return { type: 'Car', name: name, brand: brand, speed: speed };
}
// With the new shorthand form
function createSweetCar(name, brand, speed) {
return { type: 'Car', name, brand, speed }; // Yes it looks sweet.
}
Please see the compatibility table for support for these notations. In non-supporting environments, these notations will lead to syntax errors.
This shorthand notation offers object matching pretty nicely:
In ECMAScript5 what we used to do:
var tmp = getData();
var op = tmp.op;
var lhs = tmp.lhs;
var rhs = tmp.rhs;
Can be done in ECMAScript6 with a single line of code:
var { op, lhs, rhs } = getData();
var f = {a, b, c}; // <--- what exactly is this??
It defines an object in JavaScript using the new ECMAScript 2015 notation:
As per Mozilla Developer Network:
"Objects can be initialized using new Object(), Object.create(), or using the literal notation (initializer notation). An object initializer is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({})."
var a = "foo",
b = 42,
c = {};
// Shorthand property names (ES6)
var o = { a, b, c };
is equivalent to:
var a = "foo",
b = 42,
c = {};
var o = {
a: a,
b: b,
c: c
};
本文标签: ecmascript 6Javascript object literal what exactly is abcStack Overflow
版权声明:本文标题:ecmascript 6 - Javascript object literal: what exactly is {a, b, c}? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736983870a1958569.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
{"a" : 1, "b" : "x", "c" : true }
– Benjamin Gruenbaum Commented Dec 22, 2015 at 12:38