What does {var1, var2}?

I came across this example in the Mozilla documentation.


I don’t understand what kind of data is stored inside the curly brackets. It is not an object - since the elements don’t have a key, and it is not an array, since it is not stored in square brackets.
Thanks in advance,
Ben Carp

1 Like

It depends on what you do with the function. If you would execute it like a standard function like this var bob = Person(“Bob”,“Smith”); then it wouldn’t make much sense but if you treat it like a class and execute it like this var bob = new Person(“Bob”,“Smith”); then it gets turned into Object and “first” and “last” arguments get assigned to “this.name”. When you instantiate Person() class, at the execution time you will receive this object:
this.name = {
first: “Bob”,
last: “Smith”
}

1 Like
var a = "foo", 
    b = 42,
    c = {};

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

With ES6 you can do it this way:

var a = "foo", 
    b = 42, 
    c = {};

// Shorthand property names (ES2015)
var o = { a, b, c };

// In other words,
console.log((o.a === { a }.a)); // true

It’s just a shorthand, examine this code block and you’ll underdstand it

var name = "ben"
var last = "ten"
var i = {
  name,
  last
}
var j = {
  name: name,
  last: last
}
console.log(i) // Object {  last: "ten", name: "ben"}
console.log(j) // Object {  last: "ten", name: "ben"}
1 Like

In ES6 you don’t have to write:

this.name = {
  first: first,
  last: last
}

It is implied that you’re setting the value in the object to the input of the same name.

2 Likes

Thanks everyone!
I really appreciate it.

1 Like