endow
v0.3.1
Published
A minimalistic, yet powerful, class based mixin utility.
Downloads
9
Maintainers
Readme
endow
A minimalistic, yet powerful, class based mixin utility.
Following an approach explained in this post,
and inspired by one piece of the following utility,
endow makes creation of mixins a no-brainer, giving the ability to use instanceof
operator.
// define mixins through this class based pattern
const Mixin1 = Super => class extends Super {
behavior1() {}
};
const Mixin2 = Super => class extends Super {
behavior2() {}
};
// endow classes with mixins while extending
class Sub extends endow(Super).with(Mixin1, Mixin2) {
behaviors() {
this.behavior1();
this.behavior2();
}
}
new Sub instanceof Sub; // true
new Sub instanceof Super; // true
new Sub instanceof Mixin1; // true
new Sub instanceof Mixin2; // true
How to include
- ESM via
import endow from 'endow/esm'
- CJS via
const {endow} = require('endow/cjs');
- browsers via unpkg.com/endow
Compatibility
This module syntax is purposely written in an ES3 compatible fashion.
ES5 Array#some and Symbol are needed,
and to have a proper instanceof
operator result, the Symbol.hasInstance is needed too,
but it's not mandatory.
Please note this utility works with all polyfills (loaded upfront) and transpilers too.
Size
- plain
index.js
is 2535 bytes - plain
min.js
is 499 bytes - gzip
min.js
is 337 bytes - brotli
min.js
is 277 bytes