UNPKG

961 BJavaScriptView Raw
1const mixin = (behaviour, sharedBehaviour = {}) => {
2 const instanceKeys = Reflect.ownKeys(behaviour);
3 const sharedKeys = Reflect.ownKeys(sharedBehaviour);
4 const typeTag = Symbol("isa");
5 function _mixin(clazz) {
6 for (let key of instanceKeys) {
7 const existing = Object.getOwnPropertyDescriptor(
8 clazz.prototype,
9 key
10 );
11 if (!existing || existing.configurable) {
12 Object.defineProperty(clazz.prototype, key, {
13 value: behaviour[key],
14 writable: true
15 });
16 } else {
17 }
18 }
19 Object.defineProperty(clazz.prototype, typeTag, { value: true });
20 return clazz;
21 }
22 for (let key of sharedKeys) {
23 Object.defineProperty(_mixin, key, {
24 value: sharedBehaviour[key],
25 enumerable: sharedBehaviour.propertyIsEnumerable(key)
26 });
27 }
28 Object.defineProperty(_mixin, Symbol.hasInstance, {
29 value: (x) => !!x[typeTag]
30 });
31 return _mixin;
32};
33export {
34 mixin
35};