Minimalist mixin helper designed to be used with ES6 (ES2015) classes. Less than 1 kB minified and gzipped.
npm install --save es6-mixin
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
import { mixin } from 'es6-mixin';
class Foo {
foo() {
return 'foo';
}
}
class Bar {
constructor() {
mixin(this, Foo);
}
}
new Bar().foo(); // => 'foo'
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'
import { mixin } from 'es6-mixin';
function Foo() {
}
Foo.prototype.foo = function() {
return 'foo';
};
class Bar {
constructor() {
mixin(this, Foo);
}
}
new Bar().foo(); // => 'foo'
import { Mixin } from 'es6-mixin';
class Foo extends Mixin {
foo() {
return 'foo';
}
}
class Bar {
constructor() {
Foo.mixin(this);
}
}
new Bar().foo(); // => 'foo'