Set default objects with Object.assign
No, you should use object spread syntax here.
config = {
title: "Foo",
body: "Bar",
buttonText: "Baz",
cancellable: true
...config};
There is a pull request for it already: #204
The pull request (#204) has been closed, it should probably be reopened.
Please clarify a bit on what makes spread syntax better than Object.assign() ?
It's simpler.
Spread syntax didn't exist when Object.assign was created, but now we have spread syntax to make things clearer and more concise.
Also, Object.assign has a footgun where calling it would call the passed object's setters, potentially triggering side effects if the setters were sloppily made.
To clarify what @spartanatreyu wrote, MDN Web Docs says:
Note that Object.assign() can be used to mutate an object, whereas spread syntax can't.
const obj1 = { foo: "bar", x: 42 }; Object.assign(obj1, { x: 1337 }); console.log(obj1); // { foo: "bar", x: 1337 }
Mutations in code are generally considered detrimental because they make it more difficult to reason about the code; you have to keep track of various states mentally. For this reason, the React framework leans towards a functional programming style, where side effects are isolated and mutations are minimized or avoided altogether.
Ever since I learned about the spread operator, I found no reason to use Object.assign anymore, I could express everything with the spread operator so far.