The _.extend lodash method works by assigning the own properties, and prototype properties of one or more objects to an object. Assigning is another term for referencing, rather than copying, or cloning. So when _.extend is used any change that might occur to nested objects in the objects that are being referenced, will also occur in the object that is extended. If this is a problem there are many other methods in lodash, such as _.merge that will deep copy the given objects, rather than just simply referencing them. I will cover this is greater detail in a latter section in this post.
For a basic example of _.extend I put together a quick example that involves an object that is made with _.create that works in a very similar fashion to that of the native Object.create. The reason why this is important for a _.extend example is that it will result in object that has a prototype object with some visible properties that will be combined when used with _.extend which sets the method apart from other alternatives such as _.assign, and _.merge.
So as you can see when I use _.extend to combine objects a and b into a new empty object, all own and inherited properties are combined, and assigned to the empty object. In a nut shell thats all there is to write about, but there are some additional pit falls to cover, when it comes to dealing with nested objects for instance.
_.extend is very similar to _.assign, it works in almost the same way only it does not merge in prototype methods. In fact _.extend is just an alias for _.assignIn. So compared to _.extend it will do the same thing, but without combining in prototype key name values.
The _.merge method also works like _.extend, but it will deep clone objects, rather than just simply referencing them. In some cases this might be preferred if for some reason I want everything to be copied into new objects that can be manipulated without changing the state of the objects from which they are created, and vis versa. In other cases it is not needed, or will actually result in undesired behavior as I do in fact want to work with references, as such _.extend, or _.assign would be the better choice.
Two major methods to start out with here might be Object.create, as well as the Object.getPrototypeOf methods. Another useful Object method to be aware of is the Object keys method that will return an array of public key values for an object so this can be used as a way to know what properties of an object are own properties of an object, so this object keys method alone can be used as a kind of inverse of using object keys with this object get prototype method then.
When it comes to doing what the lodash extend method does there is a native Object assign method, however using just that alone will not do what the lodash extend method does, as this method will just assign own properties of the objects. However if you can just simply get the desired prototype object, then adding it into the mix just like with extend would just have to involve using the Object.getPrototypeOf method for an object on top of just passing the object alone.
So that is my post on _.extend for now, as my content on lodash continues to grow I will likely come back to this post to revise and expand on the content. It might be a good idea to add some vanilla js alternatives to _.extend, or give some more detailed examples of its use. If there is anything you might like me to add, be sure to let me know in the comments section. Thanks for reading.