0
# inherits
1
2
Browser-friendly inheritance fully compatible with standard node.js inherits(). This package provides a lightweight alternative to requiring the full util module in browser environments, automatically falling back to Node.js util.inherits when available or using a standalone browser implementation when needed.
3
4
## Package Information
5
6
- **Package Name**: inherits
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install inherits`
10
11
## Core Imports
12
13
CommonJS:
14
```javascript
15
const inherits = require("inherits");
16
```
17
18
Note: This package uses CommonJS exports and does not provide native ESM support. For ESM projects, use dynamic imports or a bundler that handles CommonJS modules.
19
20
## Basic Usage
21
22
```javascript
23
const inherits = require("inherits");
24
25
// Define parent constructor
26
function Animal(name) {
27
this.name = name;
28
}
29
30
Animal.prototype.speak = function() {
31
console.log(`${this.name} makes a sound`);
32
};
33
34
// Define child constructor
35
function Dog(name, breed) {
36
Animal.call(this, name); // Call parent constructor
37
this.breed = breed;
38
}
39
40
// Set up inheritance
41
inherits(Dog, Animal);
42
43
// Add child-specific methods
44
Dog.prototype.speak = function() {
45
console.log(`${this.name} barks`);
46
};
47
48
Dog.prototype.wagTail = function() {
49
console.log(`${this.name} wags tail`);
50
};
51
52
// Create instance
53
const myDog = new Dog("Rex", "Golden Retriever");
54
myDog.speak(); // "Rex barks"
55
myDog.wagTail(); // "Rex wags tail"
56
57
console.log(myDog instanceof Dog); // true
58
console.log(myDog instanceof Animal); // true
59
console.log(Dog.super_ === Animal); // true
60
```
61
62
## Architecture
63
64
The inherits package uses environment detection to provide the most appropriate inheritance implementation:
65
66
- **Node.js Environment**: Uses the built-in `util.inherits` function when available
67
- **Browser Environment**: Falls back to a standalone implementation that mirrors Node.js behavior
68
- **Legacy Browser Support**: Provides compatibility for browsers without `Object.create()` support
69
70
The package exports a single function that establishes prototype-based inheritance between constructor functions, following the same patterns as Node.js's standard library.
71
72
## Capabilities
73
74
### Inheritance Function
75
76
Establishes prototype-based inheritance between constructor functions, making the child constructor inherit from the parent constructor.
77
78
```javascript { .api }
79
/**
80
* Establishes prototype-based inheritance between constructor functions
81
* @param {Function} ctor - The constructor function that will inherit (child)
82
* @param {Function} superCtor - The parent constructor function to inherit from
83
* @returns {undefined} This function does not return a value
84
*/
85
function inherits(ctor, superCtor);
86
```
87
88
**Behavior:**
89
- Sets `ctor.super_` property to reference the parent constructor (allows `Child.super_` access)
90
- Establishes prototype chain so child instances inherit parent methods
91
- Maintains proper `instanceof` behavior for both child and parent types
92
- Preserves constructor property on the child prototype
93
- Compatible across Node.js and browser environments
94
95
**Parameters:**
96
- `ctor` (Function): The child constructor function that will inherit from the parent
97
- `superCtor` (Function|null): The parent constructor function to inherit from, or null to create an orphaned constructor
98
99
**Return Value:**
100
- Returns `undefined` - this function modifies the constructor functions in place
101
102
**Usage Patterns:**
103
104
```javascript
105
// Basic inheritance setup
106
function Parent() {}
107
function Child() {}
108
inherits(Child, Parent);
109
110
// With null parent (creates orphaned constructor)
111
function Orphan() {}
112
inherits(Orphan, null);
113
114
// Typical OOP pattern
115
function EventEmitter() {}
116
EventEmitter.prototype.on = function(event, listener) { /* ... */ };
117
118
function MyClass() {
119
EventEmitter.call(this); // Call parent constructor
120
}
121
inherits(MyClass, EventEmitter);
122
123
// Child now has access to parent methods
124
const instance = new MyClass();
125
instance.on('event', callback); // Inherited method
126
```
127
128
**Environment Compatibility:**
129
130
The function behavior is identical across environments:
131
132
- **Node.js**: Uses `util.inherits` when available
133
- **Modern Browsers**: Uses `Object.create()` with property descriptors
134
- **Legacy Browsers**: Uses temporary constructor function pattern
135
136
**Post-Inheritance Properties:**
137
138
After calling `inherits(Child, Parent)`:
139
140
- `Child.super_` references `Parent` constructor
141
- `Child.prototype` inherits from `Parent.prototype`
142
- `new Child() instanceof Parent` returns `true`
143
- `new Child() instanceof Child` returns `true`
144
- `Child.prototype.constructor` equals `Child`