Avoiding Object mutations with Object.assign() and ...spread

In notebook:
Egghead Redux
Created at:
2016-07-09
Updated:
2016-07-09
Tags:
pattern libraries JavaScript
Shows the naive toggleTodo implementation
  const toggleTodo = (todo) => {
  todo.completed = !todo.completed;
  return todo;
}
Instead, to not to mutate, the object, but still keep the function generic:
  return Object.assign({}, todo, {
  completed: !todo.completed
});
Here (above), he passes an empty object to assign, to make sure to not to mutate the data (​Object.assign({}...​. 

Then you add several objects to merge into this empty object. Here there's ​todo​ and the property we want to override.