Lesson 10. Object.assign
After reading lesson 10, you will
- Know how to set default values with Object.assign
- Know how to extend objects with Object.assign
- Know how to prevent mutations when using Object.assign
- Understand how Object.assign assigns values
Libraries like Underscore.js and Lodash.js have become like the utility belts of JavaScript. They add solutions to common tasks so that the average developer doesn’t need to reinvent the wheel for every project. Some of these tasks become so ubiquitous and well-defined that it makes sense to actually include them in the language. Object.assign is one such method. With it you can easily set default values or extend objects in place or as copies, all without having to write a lot of boilerplate code to do so.
Consider this
JavaScript objects can inherit properties from another object via the prototype chain. But any given object can only have one prototype. How would you go about devising a way to allow objects to inherit from multiple source objects?