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