NPM inherits Package
inherits – Inherits prototype methods
Usage
Install
npm install inherits
Simple
var inherits = require('inherits');
function Parent() {
this.name = 'parent';
}
Parent.prototype.getName = function() {
return this.name;
};
function Child() {
Parent.call(this);
this.name = 'child';
}
// Note: Use call vs. apply, as apply calls the function without setting 'this'
// See: http://stackoverflow.com/a/7225989/1333928
inherits(Child, Parent);
var child = new Child();
console.log(child.getName()); // prints 'child'
ES6 Class
var inherits = require('inherits');
class Parent {
constructor() {
this.name = 'parent';
}
getName() {
return this.name;
}
}
class Child extends Parent {
constructor() {
super();
this.name = 'child';
}
}
inherits(Child, Parent);
var child = new Child();
console.log(child.getName()); // prints 'child'
API
inherits(ctor, superCtor)
Inherits the prototype methods of a superclass constructor onto a subclass constructor
Params
- ctor {Function} – the subclass constructor
- superCtor {Function} – the superclass constructor
Returns
- None
Contributing
- Clone the repository:
git clone git@github.com:isaacs/inherits.git - Install dependencies:
npm install - Run tests:
npm test - Submit a pull request
License
The MIT License (MIT)