Dear ES2015 Community, one line of code isn’t always better than two
Michael Barshinger
5

Your third example isn’t functionally equivalent; the object spread operator is used for creating a new object. Your traditional JavaScript example only mutates the same object. Since that example looks like a Redux reducer then your traditional JavaScript version doesn’t actually work.

Since there’s no native way to shallow copy in ES5, you either need to use a library (e.g. jQuery’s extend), write it yourself or polyfill ES6's Object.assign.

// traditional javascript equivalent
function reducer(state, action) {
return Object.assign(
{},
state,
{ all: action.payload.data }
);
}
Show your support

Clapping shows how much you appreciated Scott Lambert’s story.