gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:38:56 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com