mixem
mixem, mix 'em, is a module that mixes prototypes into a target prototype as a form of inheritance via decoration.
Example
var mixem = ; // mixer "class" { } Mixerprototype { return 'bar';}; // target "class" { } Targetprototype { return 'foo';}; // mix 'em; // usevar instance = ; console; // "foo"console; // "bar"
Installation
Node
To install mixem
in a Node application use npm.
$ npm install mixem
Browser
No tests available for the browser but you may try using it via webpack.
$ webpack index.js mixem.js
Test
To run tests use npm.
$ npm install
$ npm test
Documentation
Basic Usage
mixem
can be used to create multiple inheritance with only a single prototype chain. Unlike inherits
it doesn't create nested prototypes. mixem
simple borrows/mixes given prototypes on to a base prototype.
var Emitter = EventEmitter; { }MixerOneprototype { }; { }MixerTwoprototypeanotherThing { }; { Emitter; ; ;}Baseprototype { }; ; var instance = ; isntance; // Baseinstance; // MixerOneinstance; // MixerTwoinstance; // EventEmitter
Caveats
Method Priority
mixem
doesn't follow the same merge procedure as extend
. If a method already exists on a prior prototype within the given arguments it will not be overwritten.
{ }Baseprototype { return 'from base'; }; { }Mixprototype { return 'from mix'; }; ; var instance = ; console; // "from base"
Natives
Unfortuntely it doesn't work with natives like Array
s. In order to inherit from native types just follow the usual way.
{ } Baseprototype = ;Baseprototypeconstructor = Base;
Strict Constructors
If a constructor implements a strict context check you won't be able to call the constructor and bind the instance.
{ if !this instanceof StrictMixer return ; } { StrictMixer; // this won't work} ;
Mixings will still be copied and usable however the constructor won't be abe to initialise the instance correctly.
API
mixem(<Base>, [Mix...])
License
Copyright (c) 2014 Christopher Turner