我的问题最好通过 this jsfiddle 给出,其代码如下:
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 );
f
是一种什么样的数据结构?它只是 d
的简写吗?
最佳答案
var f = {a, b, c};
它与 ES6 (ECMAScript 2015) 一起提供,意思完全相同:
var f = {a: a, b: b, c: c};
它被称为 Object Literal Property Value Shorthands(或者简称为属性值简写,shorthand properties)。
您还可以将速记与经典初始化结合起来:
var f = {a: 1, b, c};
有关详细信息,请参阅 Property definitions在 Object initializer .
关于Javascript 对象字面量 : what exactly is {a, b, c}?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53124384/