Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mixin

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mixin

Prototypical mixin layer

  • 0.2.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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

FAQs

Package last updated on 28 Jun 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc