Looking over my content so far I am surprised that I have not yet wrote a post on _.assign in lodash, as well as the native alternative Object.assign methods. The _.assign method is one of many ways to go about combining a bunch of objects into a single object, and all around assign seems to work well for most situations, but there is a lot to be aware of when merging objects.
On the surface merging objects down together into one might seem to be a simple task, but often it is not so simple as one can run into all kinds of problems and concerns that have to do with things like copying be reference rather than value, and how to handle recursive references that might be present. So then the lodash assign method will work most of the time, but often there might be some other method that should be used such as merge, or one should at least clone objects that need to be cloned, or do more than just simply depend on one single method to get everything done all each time.
When it comes to objects they are copied by reference rather than value as wih primative values like numbers and strings, which can result in unexpected behavior if you are new to javaScript and are not aware of that nature surrounding objects. A new developer might assume that when they are assigning objects together they are creating a new object, and all the nested properties with values that are primitives, and additional objects, are new copies of the source objects. This might be the case when copying primitives, but it is not at all the case with objects. So the desired result of having a copy of nested source objects is not the case with the assign method.In lodash there are other options in lodash like merge, and also deep clone that can be used to get that effect if desired.
There is also the question of the prototype property of objects, and how that should be handled when combining two or more objects.
So in todays post I will be covering some use case scenarios of _.assign, and alternatives such as _.merge, and the native Object.assign method.
Read More