Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gfarrell/mixin.js
Simple and concise mixins library for javascript
https://github.com/gfarrell/mixin.js
Last synced: 13 days ago
JSON representation
Simple and concise mixins library for javascript
- Host: GitHub
- URL: https://github.com/gfarrell/mixin.js
- Owner: gfarrell
- Created: 2015-06-14T09:00:12.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-06-14T10:50:25.000Z (over 9 years ago)
- Last Synced: 2024-11-16T14:11:38.585Z (2 months ago)
- Language: JavaScript
- Size: 117 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Simple mixins for javascript
Being free of rigid class inheritance in javascript is great. Using mixins helps promote the single responsibility principle and keeps your code as DRY as possible (or necessary). This library doesn't do anything particularly fancy, it just allows you to easily create mixins and check inheritance for them. It tries to stay as close as possible to a simple `Object.assign` call, while making it slightly easier to keep everything organised.
## Usage
### Creating a mixin
var MyMixin = mixin.create('MyMixin', {
// some methods and maybe some state?
});This creates a mixin object with defined state / functionality.
### Applying a mixin
var Person = function Person(name) {
this.rename(name);
};
Person.prototype.name = null;
Person.prototype.rename = function rename(name) {
this.name = name;
};var Greeter = mixin.create('Greeter', {
greet(other) {
return `Hello, ${other}, my name is {this.name}`;
}
});Greeter.mix(Person.prototype);
// or: mixin.mix(Person.prototype, Greeter);console.log((new Person('Jane')).greet('John')); // "Hello John, my name is Jane"
Multiple mixins can be applied to a single object in one function call to `mixin.mix()`:
mixin.mix(objectToAugment, Mixin1, Mixin2, ...);
### Checking inheritance
var MyMixin = mixin.create('MyMixin', {
// ...
});MyMixin.inheritedBy(someObject); // true if MyMixin has been applied to someObject, otherwise false
This can also be checked with just the name of the mixin using `doesInherit()`:
mixin.doesInherit(someObject, MyMixin.name);
## Tests
To run tests use `npm test`
## Node version
This module uses some ES6 features, so can be used in the browser using `babel` to transform it, or requires node>=2.x