create-mixin

3.0.0 • Public • Published

view on npm npm module downloads Build Status Dependency Status js-standard-style

create-mixin

Useful for achieving something resembling multiple-inheritence in Javascript.

const mixInto = require('create-mixin')
const EventEmitter = require('events')
 
class EmittingArray extends mixInto(EventEmitter)(Array) {}

Example

Given these two classes.

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.

> const mixInto = require('create-mixin')
 
> class Something extends mixInto(Mixin)(Base) {}

Behaviour of new class.

> 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>.

Dependencies (0)

    Dev Dependencies (5)

    Package Sidebar

    Install

    npm i create-mixin

    Weekly Downloads

    60,630

    Version

    3.0.0

    License

    MIT

    Unpacked Size

    5.52 kB

    Total Files

    5

    Last publish

    Collaborators

    • 75lb