mix-in
Turns a set of functions into a mix-in/trait that can extend a class.
Installation
Requires Node.js 7.0.0 or above.
npm i mix-in
Basic Usage
Step 1: Define the Mix-In
Let’s say you want to create a module that adds enable()
and disable()
methods to a class. Your module would require('mix-in')
and look like this:
const Mixin = moduleexports =
This is all you need to do for your mix-in module. Easy enough, right? Now we’ll turn to the module that makes use of your new mix-in.
Step 2: Require and Apply the Mix-In
Let’s say you have a second module that exports a Button
class and you want to extend it with your mix-in module from step 1, which you named @lamansky/enable-disable-mix-in
.
moduleexports = // Methods go here
All you have to do in the dependent module is:
- Require the mix-in.
- Call it as a function without arguments.
- Call it again, passing the class as an argument.
Like so:
// Note the function call at the end of this line; you need to have that!const EnableDisable = moduleexports =
That’s it! Your Button
class prototype now has enable()
and disable()
methods.
Advanced Usage: Parameters
You can also create mix-ins that accept arguments. This is useful for creating configurable mix-ins, or mix-ins that access private properties using symbols.
Step 1: Define the Mix-In
Instead of defining your mix-in as an object, we’ll create a function that takes arguments. Let’s modify our previous mix-in so that it makes use of a symbol:
const Mixin = moduleexports =
Step 2: Require and Apply the Mix-In
Now do the following in the dependent module:
- Require the mix-in.
- Call it as a function, passing the arguments your mix-in is expecting.
- Call it again, passing the class as an argument.
const _enabled = Symbol'enabled'const EnableDisable = _enabled moduleexports =
Just like before, your Button
now has enable()
and disable()
in its prototype, except this time, both your Button
class and your mix-in have access to the same _enabled
symbol.