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.
Creating an Object with a Given Prototype:
4.2.
JavaScript
4.3.
Inheriting From a Complex Prototype:
4.4.
JavaScript
4.5.
Extending Prototype with Additional Properties:
4.6.
JavaScript
4.7.
Prototype Chaining with _.create():
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.create() differ from using new with a constructor?
5.2.
Can _.create() handle null prototypes?
5.3.
Is _.create() suitable for creating complex class-like structures?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.create() Method

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

Introduction

Object creation and prototype inheritance are fundamental concepts in JavaScript. Lodash enhances these capabilities with its _.create() method. This method creates a new object with a given prototype, optionally including properties. 

Lodash _.create() Method

It simplifies the process of setting up prototype-based inheritance, which is especially useful in object-oriented programming patterns and when creating new objects that share a common prototype.

Why This Function is Used

The _.create() function is used to create a new object with a specified prototype and, optionally, additional properties. This method is essential for object-oriented programming in JavaScript, where you want to create new instances that inherit from a prototype but also might need to include specific properties. It provides a clear and concise way to set up prototype inheritance without directly manipulating the prototype property of constructors.

Syntax, Parameter and Return Value

Syntax: 

_.create(prototype, [properties])

Parameters:

  • prototype (Object): The object to inherit from.
     
  • [properties] (Object): The properties to assign to the object.

Return Value: 

(Object) - Returns the new object.

Examples 

Creating an Object with a Given Prototype:

  • JavaScript

JavaScript

var _ = require('lodash');

function Shape() {

 this.type = 'shape';

}

Shape.prototype.displayType = function() {

 return this.type;

};

var circle = _.create(Shape.prototype, { 'type': 'circle' });

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

Output: 

'circle'


Demonstrates creating a new object (circle) that inherits from Shape prototype.

Inheriting From a Complex Prototype:

  • JavaScript

JavaScript

var personPrototype = {

 greet: function() {

   return `Hello, my name is ${this.name}`;

 }

};

var person = _.create(personPrototype, { 'name': 'John Doe' });

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

Output: 

'Hello, my name is John Doe'


Shows how to create an object that inherits methods from a more complex prototype.

Extending Prototype with Additional Properties:

  • JavaScript

JavaScript

var animalPrototype = {

 getType: function() {

   return `Animal type: ${this.type}`;

 }

};

var dog = _.create(animalPrototype, { 'type': 'Dog', 'bark': function() { return 'Woof!'; } });

console.log(dog.getType());

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

   Output:

'Animal type: Dog'
 'Woof!'


An example of creating a new object with both inherited and new properties.

Prototype Chaining with _.create():

  • JavaScript

JavaScript

var baseObject = { baseMethod: function() {} };

var derivedObject = _.create(baseObject);

derivedObject.newMethod = function() {};

console.log(typeof derivedObject.baseMethod);

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

Output: 

'function'
'function'


Demonstrates using _.create() to establish prototype chaining where derivedObject inherits from baseObject.

Frequently Asked Questions

How does _.create() differ from using new with a constructor?

_.create() sets up an object with a specified prototype without needing to define a constructor function. It's useful for cases where you don't need the full capabilities of a constructor but still want prototype-based inheritance.

Can _.create() handle null prototypes?

Yes, _.create() can create an object with a null prototype, effectively creating a "dictionary" object without inherited Object properties.

Is _.create() suitable for creating complex class-like structures?

While _.create() is useful for simple to moderately complex inheritance structures, traditional constructor functions or ES6 classes might be better suited for more complex or extensive class-like structures.

Conclusion

Lodash's _.create() method offers a straightforward approach to creating new objects that inherit from a specified prototype, with the optional addition of new properties. It's a valuable tool in JavaScript for setting up prototype-based inheritance in a readable and maintainable way.

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