Okiba / class-utils
Utilities that operate on classes
__
Installation
npm i --save @okiba/class-utils
Or import it directly in the browser
<script type="module" src="https://unpkg.com/@okiba/class-utils/index.js"></script>
Usage
import class-utils from '@okiba/class-utils'
🛑
Untranspiled code Okiba Core packages are not transpiled, so don't forget to transpile them with your favourite bundler. For example, using Babel with Webpack, you should prevent imports from okiba to be excluded from transpilation, like follows:
{
test: /\.js$/,
exclude: /node_modules\/(?!(@okiba)\/).*/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
mixin(BaseClass, context, Arguments)
Mixes properties and methods from a class into a given this
context
class Fruit {
constructor() {
this.isPeeled = false
}
peel() {
this.isPeeled = true
}
}
class Coloured {
constructor(color) {
this.color = color
}
}
class Edible {
constructor(color) {
mixin(Fruit, this)
mixin(Coloured, this, color)
}
}
const edible = new Edible('red')
edible.peel()
console.log(edible.isPeeled, edible.color)
// Logs: true, 'red'
Arguments
BaseClass
: Class
+ The class definition to mix-in
context
: Object
+ The context that has to include methods and props
Arguments
: any
+ to pass to the BaseClass constructor