# mixin

> Prototypical mixin layer

Latest version **0.2.0** (published 2012-06-28) · 0 weekly downloads

## Install

```sh
npm install mixin
pnpm add mixin
yarn add mixin
bun add mixin
```

## Health

**Score 10/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2012-06-28 |
| First published | 2011-06-08 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 0.4.0 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Lee Iverson |
| Maintainers | leei |

## Links

- npm: https://www.npmjs.com/package/mixin
- npm.io page: https://npm.io/package/mixin

## Recent versions

- 0.2.0 (latest) — 2012-06-28
- 0.1.0 — 2011-06-08

## README

# Mixin.js

Mixin is an easy way to repeatedly mix functionality into a prototypical
JavaScript class. It automatically takes care of the combination of overriding
prototype methods and invoking constructors. Moreover, it will notify a mixed
in constructor that is has been mixed into another class. This can be used to
construct dependent mixin hierarchies.

## Installation

Using npm:

    npm install mixin

## Usage:


    var mixin = require("mixin");

    function Foo() {
    }

    Foo.prototype = {
       t1: function() { return 't1'; }
    };

    Foo = mixin(Foo, EventEmitter);

this is the equivalent of:

    function Foo() {
        EventEmitter.call(this);
    }

    Foo.prototype = Object.create(EventEmitter.prototype);

except that I can define a full set of prototypes in the `Foo.prototype`
statement before I invoke the mixin.

# Usage

## mixin(base, mixed)

The call `mixin(base, mixed)` returns a new constructor that adds the
prototype for `mixed` at the back of the prototype chain for `base` and
invokes the constructors for both `base` and `mixed` in reverse order. If
`mixed` has function property `included`, then this function will be invoked
with `this = mixed` and the new constructor class as the single argument.

A constructor to be mixed in cannot have a prototype chain of its own
(i.e. it can't itself be the product of a mixin), however this functionality
can be achieved by calling mixin again inside the `included` callback.
Moreover, the same constructor can not be mixed in twice to the same
prototypical inheritance chain.

## ctor.included(base)

If `ctor` is mixed in to another constructor (e.g. by invoking `mixin(Base, ctor)`) then `ctor.included(base)` is called with the `this` set to the mixed in constructor (e.g. `ctor`) and is passed a single argument, the new constructor created by `mixin()`.  This is extremely useful for creating dependent chains of mixins (i.e. `Mixin1` requires `Mixin2`) since `mixin` itself can be invoked from within the `included` call.

If `included` returns a value, then it is used as the constructor for the call to `mixin` that invoked this function.  This behavior leads to the useful idiom:

    Foo.included = function(ctor) {
        return mixin(ctor, EventEmitter);
    }

to mix the functionality of EventEmitter into any constructor that mixes in `Foo`.

## mixin.alias(obj, name, suffix, fun)

Can be used to override a method already defined on `obj`. It assigns `fun` to
`obj[name+'_with_"+suffix]` and reassigns the current value of `obj[name]` to
`obj[name+'_without_'+suffix]`. Finally it sets `obj[name]` to `fun`.  This is
similar to Rails' 'alias_method_chain`.

---
_Source: https://npm.io/package/mixin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
