Objects Properties and ...
As we saw in the “Too Many, Too Few, Just Enough” section of Chapter 2, the ...
operator is pretty obvious in how it relates to spreading or gathering arrays. But what about objects?
Such a feature was considered for ES6, but was deferred to be considered after ES6 (aka “ES7” or “ES2016” or …). Here’s how it might work in that “beyond ES6” timeframe:
var o1 = { a: 1, b: 2 },
o2 = { c: 3 },
o3 = { ...o1, ...o2, d: 4 };
console.log( o3.a, o3.b, o3.c, o3.d );
// 1 2 3 4
The ...
operator might also be used to gather an object’s destructured properties back into an object:
var o1 = { b: 2, c: 3, d: 4 };
var { b, ...o2 } = o1;
console.log( b, o2.c, o2.d ); // 2 3 4
Here, the ...o2
re-gathers the destructured c
and d
properties back into an o2
object (o2
does not have a b
property like o1
does).
Again, these are just proposals under consideration beyond ES6. But it’ll be cool if they do land.