https://github.com/webreflection/endow
A minimalistic, yet powerful, class based mixin utility.
https://github.com/webreflection/endow
Last synced: about 1 year ago
JSON representation
A minimalistic, yet powerful, class based mixin utility.
- Host: GitHub
- URL: https://github.com/webreflection/endow
- Owner: WebReflection
- License: isc
- Created: 2018-03-15T20:42:20.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-07-03T17:19:46.000Z (about 8 years ago)
- Last Synced: 2025-05-11T15:17:53.235Z (about 1 year ago)
- Language: HTML
- Size: 36.1 KB
- Stars: 22
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# endow [](https://coveralls.io/github/WebReflection/endow?branch=master) [](https://travis-ci.org/WebReflection/endow) [](https://opensource.org/licenses/ISC)
A minimalistic, yet powerful, class based mixin utility.
Following an approach explained in [this post](http://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/),
and inspired by one piece of [the following utility](https://github.com/justinfagnani/mixwith.js#mixwith),
_endow_ makes creation of mixins a no-brainer, giving the ability to use `instanceof` operator.
```js
// 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](https://unpkg.com/endow)
### Compatibility
This module syntax is purposely written in an ES3 compatible fashion.
ES5 [Array#some](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) and [Symbol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) are needed,
and to have a proper `instanceof` operator result, the [Symbol.hasInstance](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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