Skip to content

Instantly share code, notes, and snippets.

@tylerthebuildor
Created February 23, 2016 17:26
Show Gist options
  • Save tylerthebuildor/ae2586afab801c0bdb94 to your computer and use it in GitHub Desktop.
Save tylerthebuildor/ae2586afab801c0bdb94 to your computer and use it in GitHub Desktop.
function mixin (behaviour, sharedBehaviour = {}) {
const instanceKeys = Reflect.ownKeys(behaviour);
const sharedKeys = Reflect.ownKeys(sharedBehaviour);
const typeTag = Symbol('isa');
function _mixin (target) {
for (let property of instanceKeys)
Object.defineProperty(target, property, { value: behaviour[property] });
Object.defineProperty(target, typeTag, { value: true });
return target;
}
for (let property of sharedKeys)
Object.defineProperty(_mixin, property, {
value: sharedBehaviour[property],
enumerable: sharedBehaviour.propertyIsEnumerable(property)
});
Object.defineProperty(_mixin, Symbol.hasInstance, {
value: (i) => !!i[typeTag]
});
return _mixin;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment