The lodash _.mixin method
The process of combining objects in lodash, or in javaSript in general actually can prove be a little tricky. There are the own properties of an object, it’s prototype object including any inherited objects, as well as even hidden properties in some cases that can be added by way of the Object.definePropery method.
When it comes to using lodash there is the idea of extending lodash with custom methods that are not a part of lodash out of the box. The lodash _.mixin method can be used to extend lodash when one wants to do this sort of thing, however it can also be used with another object also. So the lodash mixin method can be called with just one argument, and in that case the first argument should be an object of methods that are to be added to lodash. When used to two or more arguments the first argument becomes a target object other than lodash to extent with methods. It is one of many methods in lodash that can be used to combine objects, in some cases it might be useful so lets take a look at _.mixin.
The lodash mixin methods and What to know first
1 - Using _.mixin to extend lodash
One of the features of _.mixin is that it can be used to extend lodash if just a source object is given. This may more may not be a good idea depend on how you look at it. By adding my own custom methods to lodash I am turning lodash into something other than lodash. So it might not be the best move when it comes to readability of code. Many developers may see the underscore and assume that the custom method might be part of the official lodash utility library when it is not. For this reason when I make my own utility libraries I attach everything g to a global variable named something other than underscore Never the less the mixin method can be used to extend lodash.
2 - Using _.mixin to extend an object
Say you have an object that has some properties and another object of methods that will work with those properties. If two arguments are given to _.mixin the first argument id expected to be a detestation object, and the second should be the source object that has the methods that are to be used with that object.
3.1 - Just adding functions to an object
3.2 - Appending the prototype object of a function
In this example I made another vanilla javaScriot mix in method that will append to the prototype object of a given target object, but only if that object is a function. Else if the target object is not a function it will just make the source methods own properties of the target object which is the same as before.
4 - Conclusion