Home >Web Front-end >JS Tutorial >What's the Difference Between `{a: a, b: b, c: c}` and `{a, b, c}` in JavaScript Object Literals?

What's the Difference Between `{a: a, b: b, c: c}` and `{a, b, c}` in JavaScript Object Literals?

Barbara Streisand
Barbara StreisandOriginal
2024-12-27 18:56:09805browse

What's the Difference Between `{a: a, b: b, c: c}` and `{a, b, c}` in JavaScript Object Literals?

Object Literals in JavaScript: Unveiling the Enigma of {a, b, c}

Imagine the following code:

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??

A question arises: what kind of data structure is f? Is it a mere shortcut for d, or something more?

To resolve this enigma, let's explore the concept of Object Literal Property Value Shorthands. Introduced in ES6, this syntax offers a concise way to construct object literals:

var f = {a, b, c};

This is equivalent to:

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

In other words, f is an object literal that initializes its properties with the values of the existing variables a, b, and c.

Additionally, shorthands can be combined with classical property initialization:

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

For a comprehensive understanding, refer to the documentation on Property definitions in Object initializer.

The above is the detailed content of What's the Difference Between `{a: a, b: b, c: c}` and `{a, b, c}` in JavaScript Object Literals?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn