Lodash _.extend() Method

The _.extend() method is like the _.assign() method except that it iterates over its own and inherited source properties. 

Syntax:

_.extend(object, sources)

Parameters: This method accepts two parameters as mentioned above and described below:

  • object: This parameter holds the destination object.
  • sources: This parameter holds the source objects.

Return Value: This method returns an object.

Example 1:



Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining Lodash variable 
const _ = require('lodash'); 
  
function Gfg() { 
this.a = 5; 
  
function GFG() { 
this.c = 10; 
  
Gfg.prototype.b = 15; 
GFG.prototype.d = 20; 
  
// Calling _.extend() methjod 
console.log(_.extend( { a: 4 }, 
    new Gfg, new GFG));

chevron_right


Output:

{ a: 5, b: 15, c: 10, d: 20 }

Example 2:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining Lodash variable 
const _ = require('lodash'); 
  
function Gfg() { 
this.a = 5; 
  
function GFG() { 
this.c = 10; 
  
// Calling _.extend() method 
console.log(_.extend( { a: 4 }, 
    new Gfg, new GFG));

chevron_right


Output:

{ a: 5, c: 10 }

Note: This will not work in normal JavaScript because it requires the lodash library to be installed.

full-stack-img

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.