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.
Extending an Object with Prototype Properties:
4.6.
JavaScript
4.7.
Assigning Functions and Properties Together:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.assignIn() differ from _.assign()?
5.2.
Does _.assignIn() perform deep cloning?
5.3.
Is _.assignIn() safe to use with all objects?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.assignIn() Method

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In JavaScript, merging properties of objects, including inherited properties, is a task often encountered in object-oriented programming and data manipulation. Lodash's _.assignIn() (also known as _.extend()) method extends the functionality of _.assign() by copying own and inherited enumerable string keyed properties from source objects to a target object. 

This method is particularly useful for combining properties and behaviors from multiple objects, including those inherited through prototypes.

Why This Function is Used

The _.assignIn() function is used to assign properties from one or more source objects (including inherited properties) to a target object. This is essential in situations where you want not only the object's own properties but also those it inherits from its prototype chain. It is commonly used in scenarios involving object extensions, mixins, or when working with objects that have a complex prototype hierarchy.

Syntax, Parameter and Return Value

Syntax:

_.assignIn(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' };

_.assignIn(childObject, sourceObject);

console.log(childObject);
You can also try this code with Online Javascript Compiler
Run Code

Output: 

{ inheritedProperty: 'inherited', ownProperty: 'own' }


Demonstrates _.assignIn() merging both own and inherited properties.

Combining Multiple Objects:

  • JavaScript

JavaScript

var defaults = { a: 1, b: 2 };

var additionalProps = { b: 3, c: 4 };

var inheritedProps = Object.create({ d: 5 });

var result = _.assignIn({}, defaults, additionalProps, inheritedProps);

console.log(result);
You can also try this code with Online Javascript Compiler
Run Code

 Output: 

{ a: 1, b: 3, c: 4, d: 5 }


Shows merging properties from multiple sources, including inherited ones.

Extending an Object with Prototype Properties:

  • JavaScript

JavaScript

var base = { method1: function() {} };

var derived = Object.create(base);

derived.ownMethod = function() {};

var extended = _.assignIn({ newProp: 'value' }, derived);

console.log(extended.hasOwnProperty('method1'));
You can also try this code with Online Javascript Compiler
Run Code

Output: 

true


An example of extending an object with properties from another object that has prototype methods.

Assigning Functions and Properties Together:

  • JavaScript

JavaScript

var target = {};

var source1 = { prop: 'value' };

function source2() { this.func = function() {}; }

_.assignIn(target, source1, new source2());

console.log(target);
You can also try this code with Online Javascript Compiler
Run Code

Output: 

{ prop: 'value', func: [Function] }


Demonstrates using _.assignIn() to combine properties and functions from different sources into a single object.

Frequently Asked Questions

How does _.assignIn() differ from _.assign()?

While _.assign() copies only the object's own properties, _.assignIn() copies both own and inherited properties from the prototype chain.

Does _.assignIn() perform deep cloning?

No, similar to _.assign(), _.assignIn() performs a shallow copy. It does not clone nested objects but copies their references.

Is _.assignIn() safe to use with all objects?

Caution is advised when using _.assignIn(), especially with objects that have a complex prototype chain, as it might lead to unintended side effects due to copying inherited properties.

Conclusion

Lodash's _.assignIn() method is a versatile tool for merging both own and inherited properties from source objects into a target object. It is especially useful in object-oriented programming where extensions and mixins are common, providing a straightforward approach to combine behaviors and properties from multiple objects.

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