https://github.com/75lb/create-mixin
For achieving something resembling multiple-inheritence in Javascript
https://github.com/75lb/create-mixin
Last synced: 12 months ago
JSON representation
For achieving something resembling multiple-inheritence in Javascript
- Host: GitHub
- URL: https://github.com/75lb/create-mixin
- Owner: 75lb
- License: mit
- Created: 2018-05-27T11:46:44.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-09-03T13:31:45.000Z (almost 3 years ago)
- Last Synced: 2025-08-09T03:55:21.613Z (12 months ago)
- Language: JavaScript
- Homepage:
- Size: 47.9 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.npmjs.org/package/create-mixin)
[](https://www.npmjs.org/package/create-mixin)
[](https://travis-ci.org/75lb/create-mixin)
[](https://david-dm.org/75lb/create-mixin)
[](https://github.com/feross/standard)
# create-mixin
Useful for achieving something resembling multiple-inheritence in Javascript.
```js
const mixInto = require('create-mixin')
const EventEmitter = require('events')
class EmittingArray extends mixInto(EventEmitter)(Array) {}
```
## Example
Given these two classes.
```js
class Base {
constructor () {
this.ranBaseConstructor = true
}
baseMethod () {
return 1
}
}
class Mixin {
constructor () {
this.ranMixinConstructor = true
}
someMethod () {
return 2
}
}
```
Create a new class mixing one class into another.
```js
> const mixInto = require('create-mixin')
> class Something extends mixInto(Mixin)(Base) {}
```
Behaviour of new class.
```js
> const something = new Something()
> /* new class has methods of both source classes */
> something.baseMethod()
1
> something.someMethod()
2
> /* Only the base constructor is run */
> something.ranBaseConstructor
true
> something.ranMixinConstructor
undefined
> something instanceof Base
true
> something instanceof Mixin
false
```
* * *
© 2018-19 Lloyd Brookes \<75pound@gmail.com\>.