0.0.1 • Published 10 years ago

mixem v0.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
10 years ago

mixem

mixem, mix 'em, is a module that mixes prototypes into a target prototype as a form of inheritance via decoration.

Build Status

Example

var mixem = require('mixem');

// mixer "class"
function Mixer() { }

Mixer.prototype.bar = function() {
  return 'bar';
};

// target "class"
function Target() { }

Target.prototype.foo = function() {
  return 'foo';
};

// mix 'em
mixem(Target, Mixer);

// use
var instance = new Target();

console.log(instance.foo()); // "foo"
console.log(instance.bar()); // "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 = require('events').EventEmitter;

function MixerOne() { }
MixerOne.prototype.somethingElse = function() { };

function MixerTwo() { }
MixerTwo.prototype.anotherThing function() { };

function Base() {
  Emitter.call(this);
  MixerOne(this);
  MixerTwo(this);
}
Base.prototype.something = function() { };

mixem(Base, Emitter, MixerOne, MixerTwo);

var instance = new Base();

isntance.something(); // Base
instance.somethingElse(); // MixerOne
instance.anotherThing(); // MixerTwo
instance.emit('init'); // 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.

function Base() { }
Base.prototype.foo = function () { return 'from base'; };

function Mix() { }
Mix.prototype.foo = function () { return 'from mix'; };

mixem(Base, Mix);

var instance = new Base();

console.log(instance.foo()); // "from base"

Natives

Unfortuntely it doesn't work with natives like Arrays. In order to inherit from native types just follow the usual way.

function Base() { }

Base.prototype = [];
Base.prototype.constructor = Base;

Strict Constructors

If a constructor implements a strict context check you won't be able to call the constructor and bind the instance.

function StrictMixer() {
  if (!(this instanceof StrictMixer)) {
    return new StrictMixer();
  } 
}

function Base() {
  StrictMixer.call(this); // this won't work
}

mixem(Base, StrictMixer);

Mixings will still be copied and usable however the constructor won't be abe to initialise the instance correctly.

API

mixem(<Base>, Mix...)

License

MIT

Copyright (c) 2014 Christopher Turner