create-mixin
Creates a mixin for use in a class extends expression
Last updated 6 years ago by 75lb .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install create-mixin 
SYNC missed versions from official npm registry.

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

Current Tags

  • 3.0.0                                ...           latest (5 years ago)

6 Versions

  • 3.0.0                                ...           5 years ago
  • 2.0.1                                ...           6 years ago
  • 2.0.0                                ...           6 years ago
  • 1.1.1                                ...           6 years ago
  • 1.1.0                                ...           6 years ago
  • 1.0.0                                ...           7 years ago
Maintainers (1)
Downloads
Today 1
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (2)

Copyright 2013 - present © cnpmjs.org | Home |