How Lodash _.create Establishes Prototype Chains
The Lodash _.create method provides a streamlined way to
implement prototypal inheritance in JavaScript by generating a new
object with a specified prototype and optional custom properties. This
article breaks down how _.create links objects via the
internal prototype chain, how it assigns own properties, and how its
behavior compares to native JavaScript inheritance patterns.
The Mechanism Behind
_.create
In JavaScript, objects inherit properties and methods through a
reference chain known as the prototype chain. Every object has an
internal slot, typically denoted as [[Prototype]], which
points to either another object or null.
The syntax for the Lodash function is:
_.create(prototype, [properties])When called, _.create performs two primary
operations:
- Allocates a new object with the target prototype:
Internally, Lodash utilizes native
Object.create(prototype). This sets the hidden[[Prototype]]of the newly created instance directly to theprototypeargument passed to the function. - Assigns own properties: If a secondary
propertiesobject is supplied, Lodash assigns those properties directly to the new instance as own, enumerable properties using an internal assignment utility (similar toObject.assign).
Prototype Resolution in Action
Once _.create returns the object, any property lookup
follows the standard ECMAScript prototype lookup algorithm:
- Own Property Check: The JavaScript engine first checks if the requested property exists directly on the newly created object.
- Delegation Up the Chain: If the property is not
found on the instance, the engine traverses the
[[Prototype]]link to inspect the prototype object passed into_.create. - Termination: This delegation continues until the
property is found or the chain reaches
null(typically ending atObject.prototype).
Consider the following example:
const vehicleProto = {
drive: function() {
return 'Moving forward';
}
};
const car = _.create(vehicleProto, { wheels: 4 });
console.log(car.wheels); // 4 (Own property)
console.log(car.drive()); // 'Moving forward' (Inherited via prototype chain)
console.log(Object.getPrototypeOf(car) === vehicleProto); // trueIn this structure, car.wheels resides directly on the
car instance. Calling car.drive() fails to
find the property on car itself, so the engine delegates to
vehicleProto, where the method is resolved.
Differences
Between _.create and Object.create
While _.create relies on Object.create to
construct the prototype chain, it simplifies how own properties are
applied.
Native Object.create accepts property descriptor objects
as its second argument:
// Native Object.create
const obj = Object.create(proto, {
prop: {
value: 'test',
writable: true,
enumerable: true,
configurable: true
}
});Lodash's _.create abstracts this verbosity by allowing
standard key-value pairs:
// Lodash _.create
const obj = _.create(proto, { prop: 'test' });By pairing Object.create delegation with simple object
assignment, _.create establishes an explicit prototype
chain while keeping object initialization concise and readable.