https://github.com/soundcloud/remixin
Mixin library for Javascript
https://github.com/soundcloud/remixin
Last synced: 11 months ago
JSON representation
Mixin library for Javascript
- Host: GitHub
- URL: https://github.com/soundcloud/remixin
- Owner: soundcloud
- License: mit
- Created: 2015-01-18T22:24:08.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2023-10-19T18:02:01.000Z (over 2 years ago)
- Last Synced: 2025-08-14T15:53:09.354Z (11 months ago)
- Language: JavaScript
- Homepage:
- Size: 774 KB
- Stars: 27
- Watchers: 164
- Forks: 4
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
## Remixin [![version][npm badge]][npm] [![build status][travis badge]][travis]
Remixin is the aspect-oriented mixin library developed and in use at [SoundCloud][soundcloud]. It is inspired by Twitter's [advice.js][advice] and [Joose][joose].
For an introduction about why you'd want to use a mixin library, Angus Croll and Dan Webb from Twitter gave a good [talk about the concept][slides] and Angus [blogged on the subject][blog].
## Installation
Install the package via npm:
```shell
npm install remixin
```
And then import it:
```js
import { Mixin } from 'remixin';
```
Alternatively, download a browser-ready version from the unpkg CDN:
```html
```
([Underscore.js][underscore] is a dependency and needs to be included first.)
## Usage
- Create a new mixin using `mixin = new Mixin(modifiers)`
- Apply a mixin to an object, using `mixin.applyTo(object)`
- Pass options to a mixin which has a custom apply method using `mixin.applyTo(object, options)`
- Curry options into a mixin using `curried = mixin.withOptions(options)`
- Combine mixins by using `combined = new Mixin(mixin1, [mixin2, ...], modifiers)`
### Modifiers
When defining a mixin, there are several key words to define method modifiers:
- `before`: `{Object.}`
- defines methods to be executed before the original function. It has the same function signature (it is given the
same arguments list) as the original, but can not modify the arguments passed to the original, nor change whether
the function is executed.
- `after`: `{Object.}`
- The same as `before`, this has the same signature, but can not modify the return value of the function.
- `around`: `{Object.}`
- defines methods to be executed 'around' the original. The original function is passed as the first argument,
followed by the original arguments. The modifier function may change the arguments to be passed to the original,
may modify the return value, and even can decide not to execute the original. Given the power that this provides,
use with care!
- `requires`: `{Array.}`
- an array of property names which must exist on the target object (or its prototype). Basically defines an expected
interface.
- `requirePrototype`: `{Object}`
- this prototype should be present on the target object's prototype chain. can be used to specify what 'class'
target should be or from what prototype it should inherit from.
- `override`: `{Object.}`
- properties or methods which specifically should override the values already defined on the target object.
- `defaults`: `{Object.}`
- properties or methods which should be applied to the target object only if they do not already exist on that
object. Properties defined in the prototype chain will be overridden.
- `merge`: `{Object.