UNPKG

create-mixin

Version:

Mix the prototype of one class into another

26 lines (23 loc) 799 B
/** * Creates a mixin for use in a class extends expression. * @module create-mixin */ /** * @alias module:create-mixin * @param {class} Src - The class containing the behaviour you wish to mix into another class. * @returns {function} */ function createMixin (Src) { return function (Base) { class Mixed extends Base {} for (const propName of Object.getOwnPropertyNames(Src.prototype)) { if (propName === 'constructor') continue Object.defineProperty(Mixed.prototype, propName, Object.getOwnPropertyDescriptor(Src.prototype, propName)) } if (Src.prototype[Symbol.iterator]) { Object.defineProperty(Mixed.prototype, Symbol.iterator, Object.getOwnPropertyDescriptor(Src.prototype, Symbol.iterator)) } return Mixed } } export default createMixin