@aminya/mixto
v1.0.0-aminya.1
Published
A mixin superclass. Its subclasses have `.extend` and `.includeInto` methods to mix behavior into other objects and classes.
Downloads
4
Readme
Mixto: A simple mixin superclass
npm install @aminya/mixto
JavaScript Classes (ES6)
To create a mixin, subclass mixto:
const Mixin = require('mixto/lib/es6')
class MyMixin extends Mixin {
static classMethod() { console.log("foo"); }
instanceMethod() { console.log("bar"); }
}
Then mix into classes with .includeInto
:
class MyClass {
static initClass() {
MyMixin.includeInto(this); // adds all the mixin's class properties or prototype properties to the target class if they aren't already defined
}
}
MyClass.initClass();
MyClass.classMethod();
(new MyClass).instanceMethod();
Or extend individual objects with .extend
:
const myObject = {a: 1, b: 2};
MyMixin.extend(myObject); // adds all the mixin's prototype properties to the target object if they aren't already defined
myObject.instanceMethod();
Or build standalone instances of your 'mixin':
const standalone = new MyMixin;
standalone.instanceMethod();
CoffeeScript 1 Classes (ES5)
To create a mixin, subclass mixto:
Mixin = require 'mixto'
class MyMixin extends Mixin
@classMethod: -> console.log("foo")
instanceMethod: -> console.log("bar")
Then mix into classes with .includeInto
:
class MyClass
MyMixin.includeInto(this)
MyClass.classMethod()
(new MyClass).instanceMethod()
Or extend individual objects with .extend
:
myObject = {a: 1, b: 2}
MyMixin.extend(myObject)
myObject.instanceMethod()
Or build standalone instances of your 'mixin':
standalone = new MyMixin
standalone.instanceMethod()