Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Merging Inherited Properties:
4.2.
JavaScript
4.3.
Combining Multiple Objects:
4.4.
JavaScript
4.5.
Extension and Overriding:
4.6.
JavaScript
4.7.
Working with Prototype Properties:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.extend() differ from _.assign()?
5.2.
Is _.extend() suitable for deep merging of objects?
5.3.
What happens in case of property name conflicts?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.extend() Method

Author Rinki Deka
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In JavaScript, combining properties from one or more source objects into a target object is a common operation, especially in the context of object manipulation and configuration. Lodash's _.extend() method, which is an alias for _.assignIn(), serves this purpose by copying own and inherited enumerable string keyed properties from source objects to the target object. 

Lodash _.extend() Method

This method is particularly useful for merging properties, including inherited ones, in a clear and concise manner.

Why This Function is Used

The _.extend() (or _.assignIn()) function is used for merging properties, including inherited ones, from one or more source objects into a target object. It's essential in situations where you need to not only combine properties from an object's own properties but also from its prototype chain. This is particularly useful in object-oriented programming, where an object might inherit properties from a prototype that you also want to include.

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

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

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

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

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 DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass