-6

we use ...{} to join two objects in our project

can anyone explain to me how it works

Thanks

Manju Kb
  • 55
  • 1
  • 1
  • 6
  • Add some code here for clarity – Araphel Mar 15 '17 at 08:58
  • 1
    Possible duplicate of [What does the three dots in react do?](http://stackoverflow.com/questions/31048953/what-does-the-three-dots-in-react-do) – jonrsharpe Mar 15 '17 at 08:59
  • [Spread operator ?](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Spread_operator) – Alexander Mar 15 '17 at 09:01
  • @Alexander seems to be... Especially since arrays are supported with this syntax. – Araphel Mar 15 '17 at 09:04
  • @Alexander This syntax is not explained in the link that you provided. See [my answer](http://stackoverflow.com/questions/42805056/what-in-nodejs-javascript/42805869#42805869) below for details. – rsp Mar 15 '17 at 09:50

1 Answers1

2

Read about the Object Rest/Spread Properties for ECMAScript:

Example

let a = {a: 1, b: 2};
let b = {b: 3, c: 4};
let c = {...a, ...b};
console.log(c);

It outputs:

{
  a: 1,
  b: 3,
  c: 4
}

See DEMO on JSBin.

Support

Note: It is a Stage 3 proposal for ECMAScript. You need Babel if you want it to work in Node.

Confusion

This particular syntax that the question asks about is not yet (as of March, 2017) explained in this link that was posted in one of the comments:

because at the time of this writing it is still Stage 3 proposal for ECMAScript. The article on MDN talks only about arrays, for which the spread operator was defined in ES 2015 (ES6) and wasn't changed in ES 2017 so it's nothing new. The syntax for objects, however, is very new and is not even tracked on node.green yet (a website that tracks ES support in Node versions). It is, however, supported by Babel:

so it can be used in Node with transpilation.

rsp
  • 91,898
  • 19
  • 176
  • 156