Creating a Javascript object without explicitly defining keys?

Today I saw a piece of code that I had never seen before:

var a = 1;
var b = 2;
var c = { a, b };

This creates an object (assigned by c) that contains two keys, these are the names of the variables, and the values ​​are the values ​​of the variables, for example:

{
    a: 1,
    b: 2
}

Is this something normal? I could not find anything related to this or creating objects this way. I tested it on chrome and it worked fine, but I'm not sure if this will work in every browser.

I would like to create an object this way:

var c = { a: a, b: b };
+4
source share
1 answer

ES6, , () , .

, c = {a, b} c = {a: a, b: b}, a b .

MDN ​​ .

+8

Source: https://habr.com/ru/post/1649201/


All Articles