create()
create<
T>(prototype,properties?):T
Creates an object that inherits from the prototype object.
DEPRECATED
Use Object.create() directly instead.
Type Parametersβ
T: T extends objectβ
The type of the prototype object.
Parametersβ
prototype: Tβ
The object to inherit from.
properties?: objectβ
The properties to assign to the object.
Returns: Tβ
The new object.
See Alsoβ
Sinceβ
2.0.0
Also known asβ
create (Lodash, es-toolkit) Β· β (Remeda, Radashi, Ramda, Effect, Modern Dash, Antfu)
Exampleβ
// β Deprecated approach
const person = { greet() { return 'Hello'; } };
const fred = create(person, { name: 'Fred' });
// β
Recommended approach
const person = { greet() { return 'Hello'; } };
const fred = Object.assign(Object.create(person), { name: 'Fred' });
How it works?β
Creates an object with specified prototype.
Deprecated: Use Object.create() directly.
Native Equivalentβ
// β create(proto, props)
// β
Object.create(proto, props)
Use Casesβ
Create with prototype πβ
Create object inheriting from prototype.
const proto = { greet() { return 'Hello'; } };
const obj = Object.assign(Object.create(proto), { name: 'Fred' });
obj.greet(); // 'Hello'
Extend base objectβ
Create specialized object from base.
const animal = { speak() { return 'sound'; } };
const dog = Object.assign(Object.create(animal), {
speak() { return 'woof'; }
});