Syntax, Parameter and Return Value
Syntax:
_.extend(target, ...sources)
Parameters:
-
target (Object): The destination object.
- ...sources (Object): The source objects.
Return Value:
(Object) - Returns the modified target object.
Examples
Merging Inherited Properties:
JavaScript
var _ = require('lodash');
function Parent() {
this.inheritedProperty = 'inherited';
}
Parent.prototype.parentMethod = function() {};
var childObject = new Parent();
var sourceObject = { ownProperty: 'own' };
_.extend(childObject, sourceObject);
console.log(childObject);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ inheritedProperty: 'inherited', ownProperty: 'own' }
Demonstrates merging both own and inherited properties into an object.
Combining Multiple Objects:
JavaScript
var target = { a: 1 };
var source1 = { b: 2 };
var source2 = { c: 3 };
_.extend(target, source1, source2);
console.log(target);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ a: 1, b: 2, c: 3 }
Shows how _.extend() combines properties from multiple source objects into a target object.
Extension and Overriding:
JavaScript
var base = { name: 'Base', method: function() {} };
var extension = { name: 'Extended', newMethod: function() {} };
_.extend(base, extension);
console.log(base);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ name: 'Extended', method: [Function], newMethod: [Function] }
An example of extending an object with new properties and overriding existing ones.
Working with Prototype Properties:
JavaScript
var parent = { inheritedProp: 'parent' };
var child = Object.create(parent);
child.ownProp = 'child';
var extended = _.extend({}, child);
console.log(extended);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ ownProp: 'child', inheritedProp: 'parent' }
Demonstrates using _.extend() to copy both own and inherited properties from a child object to a new object.
Frequently Asked Questions
How does _.extend() differ from _.assign()?
While _.assign() only copies an object's own properties, _.extend() (or _.assignIn()) copies both own and inherited properties from the prototype chain.
Is _.extend() suitable for deep merging of objects?
_.extend() performs a shallow merge, meaning it only merges properties at the top level. For deep merging, a method like _.merge() should be used.
What happens in case of property name conflicts?
If multiple source objects have properties with the same name, _.extend() will overwrite the property in the target object with the last source's value for that property.
Conclusion
Lodash's _.extend() method is a versatile tool for merging properties from source objects into a target object, including inherited properties. It's particularly useful in object-oriented programming and scenarios where a comprehensive combination of properties from an object's prototype chain is required.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.