Javascript对象文字:什么是{a,b,c}? [英] Javascript object literal: what exactly is {a, b, c}?

查看:178
本文介绍了Javascript对象文字:什么是{a,b,c}?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题最好是通过这个jsfiddle 的方式给出,其代码如下: p>

  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}; //< ---这究竟是什么?

//这些都给出相同的输出:
alert(d.a +','+ d.b +','+ d.c);
alert(e [0] +','+ e [1] +','+ e [2]);
alert(f.a +','+ f.b +','+ f.c);

什么样的数据结构是 f ?这只是简单的一个简单的 d

解决方案

 code> var f = {a,b,c}; 

它与ES6(ECMAScript 2015)一起提供,并且意味着完全一样:

  var f = {a:a,b:b,c:c}; 

它被称为对象字面值属性值(或简称属性值简写,简写属性)。 p>

您还可以将shorthands与古典初始化相结合:

  var f = a:1,b,c}; 

有关更多信息,请参阅 Object initializer


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?

解决方案

var 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 Object initializer.

这篇关于Javascript对象文字:什么是{a,b,c}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆