With the help of Babel JavaScript Developers are able to use “futuristic’ features of JS ES7. With the newest additions to the language, code intent and readability have come to the forefront.
One nifty feature in the latest JS is called the object spread operator. It helps build new objects from existing objects without mutating the original.
Let’s take a look at how we might do that before the spread operator.
So it makes some sense, you want to assign an object to a new object, hence Object.assign. But with ES6 we have the spread operator with arrays, `[ newItem, …existingArray ]` and so why don’t we do the same with the object? Readability is debatable, but that will come with convention. This is a much cleaner way to write the same thing. Let’s face it, the 3 arguments need for Object.Assign are confusing.
So here’s how we would do it with the ES7 Object Spread Operator:
Super Clean!
Just be aware that if you’re using this in your code, you’ll need something like Babel to make it browser compatible. Happy Coding!
Leave a Reply