Aller au contenu principal

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