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'; }
});