Javascript 对象字面量 : what exactly is {a, b, c}?

标签 javascript ecmascript-6

我的问题最好通过 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 definitionsObject initializer .

关于Javascript 对象字面量 : what exactly is {a, b, c}?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53124384/

相关文章:

javascript - 我们是否应该在 ES6 类的原型(prototype)中包含方法以提高性能?

javascript - 使用javascript在单元格内的文本上创建一个url链接

javascript - 是否可以从 Internet Explorer 中动态插入的 QuickTime <object> 启用 DOM 事件

javascript - Knockoutjs 和动态更改模板

javascript - 按不同的键对对象数组进行排序

javascript - onPress 方法中箭头函数与普通函数的行为

javascript - Uncaught ReferenceError : DocxGen is not defined

javascript - 设置 Span 元素的初始值

javascript - 在javascript es6中将嵌套对象转换为递归数组或嵌套数组

javascript - 在 .then 函数中发送多个参数