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
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
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
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
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 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.