create
object
Creates an object that inherits from the prototype object.
Installation
Import
import { create } from '@tulx/utils';Source Code
Implementation
/**
* Creates an object that inherits from the prototype object.
*
* @param prototype - The object to inherit from.
* @param properties - The properties to assign to the object.
* @returns Returns the new object.
*
* @example
* ```ts
* function Shape() {
* this.x = 0;
* this.y = 0;
* }
* function Circle() {
* Shape.call(this);
* }
* Circle.prototype = create(Shape.prototype, {
* 'constructor': Circle
* });
* ```
*/
export function create<T extends Record<string, unknown>>(
prototype: object | null,
properties?: PropertyDescriptorMap & ThisType<T>
): T {
const obj = Object.create(prototype);
if (properties) {
Object.defineProperties(obj, properties);
}
return obj as T;
}
Example
import { create } from '@tulx/utils';
function Shape() {
this.x = 0;
this.y = 0;
}
function Circle() {
Shape.call(this);
}
Circle.prototype = create(Shape.prototype, {
'constructor': Circle
});Related Functions
assign
Assigns own enumerable string keyed properties of source objects to the destination object.
assignIn
This method is like assign except that it iterates over own and inherited source properties.
assignInWith
This method is like assignIn except that it accepts customizer which is invoked to produce the assigned values.
assignWith
This method is like assign except that it accepts customizer which is invoked to produce the assigned values.
at
Creates an array of values corresponding to paths of object.
defaults
Assigns own enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to undefined.