# inherit

> Inheritance module for Node.js and browsers

Latest version **2.2.7** (published 2018-10-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install inherit
pnpm add inherit
yarn add inherit
bun add inherit
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.7 |
| Published | 2018-10-05 |
| First published | 2011-10-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 16.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 71 |
| Author | Dmitry Filatov |
| Maintainers | dfilatov |
| Keywords | class, prototype, inheritance, mixins, static |

## Links

- npm: https://www.npmjs.com/package/inherit
- Repository: https://github.com/dfilatov/node-inherit
- Issues: https://github.com/dfilatov/node-inherit/issues
- npm.io page: https://npm.io/package/inherit

## Recent versions

- 2.2.7 (latest) — 2018-10-05
- 2.2.6 — 2016-09-06
- 2.2.5 — 2016-08-04
- 2.2.4 — 2016-07-07
- 2.2.3 — 2016-01-11
- 2.2.2 — 2014-06-04
- 2.2.1 — 2014-03-15
- 2.2.0 — 2014-03-05
- 2.1.0 — 2013-09-27
- 2.0.0 — 2013-04-12
- 1.0.4 — 2012-04-23
- 1.0.3 — 2012-04-20
- 1.0.2 — 2012-04-17
- 1.0.1 — 2012-02-21
- 1.0.0 — 2011-10-12

## README

Inherit [![NPM version](https://badge.fury.io/js/inherit.png)](http://badge.fury.io/js/inherit)
=======
This module provides some syntax sugar for "class" declarations, constructors, mixins, "super" calls and static members.

Getting Started
---------------
### In Node.js ###
You can install using Node Package Manager (npm):

    npm install inherit

### In Browsers ###
```html
<script type="text/javascript" src="inherit.js"></script>
```
It also supports RequireJS module format and [YM module](https://github.com/ymaps/modules) format.

Module has been tested in IE6+, Mozilla Firefox 3+, Chrome 5+, Safari 5+, Opera 10+.

Specification
-------------
### Creating a base class ###
````js
Function inherit(Object props);
````
### Creating a base class with static properties ###
````js
Function inherit(
    Object props,
    Object staticProps);
````
### Creating a derived class ###
````js
Function inherit(
    Function BaseClass,
    Object props,
    Object staticProps);
````
### Creating a derived class with mixins ###
````js
Function inherit(
    [
        Function BaseClass,
        Function Mixin,
        Function AnotherMixin,
        ...
    ],
    Object props,
    Object staticProps);
````

Example
------------
```javascript
var inherit = require('inherit');

// base "class"
var A = inherit(/** @lends A.prototype */{
    __constructor : function(property) { // constructor
        this.property = property;
    },

    getProperty : function() {
        return this.property + ' of instanceA';
    },
    
    getType : function() {
        return 'A';
    },

    getStaticProperty : function() {
        return this.__self.staticProperty; // access to static
    }
}, /** @lends A */ {    
    staticProperty : 'staticA',
    
    staticMethod : function() {
        return this.staticProperty;
    }
});

// inherited "class" from A
var B = inherit(A, /** @lends B.prototype */{
    getProperty : function() { // overriding
        return this.property + ' of instanceB';
    },
    
    getType : function() { // overriding + "super" call
        return this.__base() + 'B';
    }
}, /** @lends B */ {
    staticMethod : function() { // static overriding + "super" call
        return this.__base() + ' of staticB';
    }
});

// mixin M
var M = inherit({
    getMixedProperty : function() {
        return 'mixed property';
    }
});

// inherited "class" from A with mixin M
var C = inherit([A, M], {
    getMixedProperty : function() {
        return this.__base() + ' from C';
    }
});

var instanceOfB = new B('property');

instanceOfB.getProperty(); // returns 'property of instanceB'
instanceOfB.getType(); // returns 'AB'
B.staticMethod(); // returns 'staticA of staticB'

var instanceOfC = new C();
instanceOfC.getMixedProperty() // returns "mixed property from C"
```

---
_Source: https://npm.io/package/inherit · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
