Creates objects that inherit from a given prototype with optional property assignments
npx @tessl/cli install tessl/npm-lodash--create@3.1.00
# Lodash Create
1
2
The modern build of lodash's `_.create` as a standalone module. Creates objects that inherit from a given prototype with optional property assignments, enabling efficient prototypal inheritance patterns in JavaScript.
3
4
## Package Information
5
6
- **Package Name**: lodash.create
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash.create`
10
11
## Core Imports
12
13
```javascript
14
const create = require("lodash.create");
15
```
16
17
## Basic Usage
18
19
```javascript
20
const create = require("lodash.create");
21
22
// Simple prototype inheritance
23
function Shape() {
24
this.x = 0;
25
this.y = 0;
26
}
27
28
function Circle() {
29
Shape.call(this);
30
}
31
32
// Create prototype chain with additional properties
33
Circle.prototype = create(Shape.prototype, {
34
'constructor': Circle
35
});
36
37
const circle = new Circle();
38
console.log(circle instanceof Circle); // => true
39
console.log(circle instanceof Shape); // => true
40
```
41
42
## Capabilities
43
44
### Object Creation with Prototypal Inheritance
45
46
Creates an object that inherits from the given prototype object with optional property assignments.
47
48
```javascript { .api }
49
/**
50
* Creates an object that inherits from the given `prototype` object. If a
51
* `properties` object is provided its own enumerable properties are assigned
52
* to the created object.
53
*
54
* @param {Object} prototype The object to inherit from.
55
* @param {Object} [properties] The properties to assign to the object.
56
* @param {Object} [guard] Enables use as a callback for functions like `_.map`.
57
* @returns {Object} Returns the new object.
58
*/
59
function create(prototype, properties, guard)
60
```
61
62
**Parameters:**
63
- `prototype` (Object): The object to inherit from
64
- `properties` (Object, optional): The properties to assign to the object
65
- `guard` (Object, optional): Internal parameter - enables use as a callback for functions like `_.map`.
66
67
**Returns:** Object - Returns the new object that inherits from the prototype
68
69
**Usage Examples:**
70
71
```javascript
72
const create = require("lodash.create");
73
74
// Basic prototype inheritance
75
const animal = {
76
type: 'animal',
77
speak() {
78
console.log('Some generic animal sound');
79
}
80
};
81
82
const dog = create(animal, {
83
type: 'dog',
84
speak() {
85
console.log('Woof!');
86
},
87
breed: 'labrador'
88
});
89
90
console.log(dog.type); // => 'dog'
91
console.log(dog.breed); // => 'labrador'
92
dog.speak(); // => 'Woof!'
93
94
// Constructor function pattern
95
function Vehicle(wheels) {
96
this.wheels = wheels;
97
}
98
99
Vehicle.prototype.move = function() {
100
return 'Moving with ' + this.wheels + ' wheels';
101
};
102
103
function Car() {
104
Vehicle.call(this, 4);
105
}
106
107
// Set up inheritance chain
108
Car.prototype = create(Vehicle.prototype, {
109
constructor: Car,
110
honk() {
111
return 'Beep beep!';
112
}
113
});
114
115
const myCar = new Car();
116
console.log(myCar.move()); // => 'Moving with 4 wheels'
117
console.log(myCar.honk()); // => 'Beep beep!'
118
console.log(myCar instanceof Car); // => true
119
console.log(myCar instanceof Vehicle); // => true
120
121
// Creating objects without properties
122
const emptyChild = create(animal);
123
console.log(emptyChild.type); // => 'animal' (inherited)
124
emptyChild.speak(); // => 'Some generic animal sound'
125
126
// Creating from null prototype (no inherited properties)
127
const cleanObject = create(null, {
128
name: 'clean',
129
value: 42
130
});
131
132
console.log(cleanObject.name); // => 'clean'
133
console.log(cleanObject.toString); // => undefined (no Object.prototype)
134
console.log(cleanObject.hasOwnProperty); // => undefined (no Object.prototype)
135
```