mixes
A minimal util to mixin functions and properties. This helps reduce the boilerplate and repetition of MyClass.prototype
and Object.defineProperty
.
//mixin some stuff to MyClass.prototypeMyClass //mixin functions { return a + b } //mixin an object for Object.defineProperty bar: { return "boop" }
Then, you can use them as expected:
var m = mconsole
It also allows you to create collections of mixins easily:
var mixes = {}
motivation
The earlier code would look like this in pure JS, and tends to bloat as you add more functions and properties.
MyClassprototype { return a + b} Object
This is also nicer than Blah.prototype = { ... }
since it doesn't destroy your prototype chain (i.e. if you're inheriting from a base class).
Usage
mixes(ctor, entries)
For a constructor function or object with prototype
, mixes in the given functions and properties. entries
is an object with named functions or objects.
When an object is encountered, it is treated as a property and injected with Object.defineProperty
. By default, configurable
and enumerable
are true if not specified, although you can override it like so:
Other types (numbers, strings, etc) are ignored.
mixes.mix(obj, entries)
The same as above, but operates on any object as opposed to an object's prototype
.
License
MIT, see LICENSE.md for details.