Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including es6-mixin with all npm packages installed. Try it out:

var es6Mixin = require("es6-mixin")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

es6-mixin v0.3.0

Minimalist mixin helper designed to be used with ES6 (ES2015) classes.

es6-mixin

Minimalist mixin helper designed to be used with ES6 (ES2015) classes.

Latest Stable Version License Build Status

Dependency Status NPM Downloads Test Coverage API Documentation Code Climate

Installation

npm install --save es6-mixin

Usage

mix(SuperClass, Mixin1, Mixin2, ...)

import { mix } from 'es6-mixin';

class Super {
  foo() {
    return 'foo';
  }
}

class Mixin1 {
  bar () {
    return 'bar';
  }
}

class Mixin2 {
  baz () {
    return 'baz';
  }
}

class Sub extends mix(Super, Mixin1, Mixin2) {
}

new Sub().foo(); // => 'foo'
new Sub().bar(); // => 'bar'
new Sub().baz(); // => 'baz'
new Sub() instanceof Super; // => true
new Sub() instanceof Mixin1; // => false
new Sub() instanceof Mixin2; // => false

mixin(target, Mixin [, arg1, arg2, ...])

Basic usage

import { mixin } from 'es6-mixin';

class Foo {
  foo() {
    return 'foo';
  }
}

class Bar {
  constructor() {
    mixin(this, Foo);
  }
}

new Bar().foo(); // => 'foo'

Pass parameters to a constructor

import { mixin } from 'es6-mixin';

class Foo {
  constructor(a, b, c) { ... }

  foo() {
    return 'foo';
  }
}

class Bar {
  constructor() {
    mixin(this, Foo, 1, 2, 3); // 1, 2, 3 are passed to Foo's constructor
  }
}

new Bar().foo(); // => 'foo'

Use with ES5-style prototypes

import { mixin } from 'es6-mixin';

function Foo() {
}

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

class Bar {
  constructor() {
    mixin(this, Foo);
  }
}

new Bar().foo(); // => 'foo'

class extends Mixin { ... }

import { Mixin } from 'es6-mixin';

class Foo extends Mixin {
  foo() {
    return 'foo';
  }
}

class Bar {
  constructor() {
    Foo.mixin(this);
  }
}

new Bar().foo(); // => 'foo'

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free