UNPKG

@gvray/eskit

Version:

A rich and colorful toolkit about typescript and javascript.

125 lines 3.56 kB
var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; import copyProperties from './copyProperties'; /** * Creates a new class by combining multiple classes (mixin pattern). * 通过组合多个类创建新类(混入模式)。 * * @param mixins - The classes to combine / 要组合的类 * @returns A new class with combined functionality / 具有组合功能的新类 * * @example * ```typescript * // Define base classes * class Walkable { * walk() { * console.log('Walking...') * } * } * * class Swimmable { * swim() { * console.log('Swimming...') * } * } * * class Flyable { * fly() { * console.log('Flying...') * } * } * * // Create combined classes * const Duck = mixin(Walkable, Swimmable, Flyable) * const Fish = mixin(Swimmable) * const Bird = mixin(Walkable, Flyable) * * // Use the mixed classes * const duck = new Duck() * duck.walk() // 'Walking...' * duck.swim() // 'Swimming...' * duck.fly() // 'Flying...' * * const fish = new Fish() * fish.swim() // 'Swimming...' * // fish.walk() // Error: walk is not a function * * // With properties and constructor logic * class HasName { * constructor() { * this.name = 'Unknown' * } * getName() { * return this.name * } * } * * class HasAge { * constructor() { * this.age = 0 * } * getAge() { * return this.age * } * } * * const Person = mixin(HasName, HasAge) * const person = new Person() * console.log(person.getName()) // 'Unknown' * console.log(person.getAge()) // 0 * ``` * * @since 1.0.0 */ var mixin = function () { var e_1, _a; var mixins = []; for (var _i = 0; _i < arguments.length; _i++) { mixins[_i] = arguments[_i]; } var Mixin = /** @class */ (function () { function Mixin() { var e_2, _a; try { for (var mixins_2 = __values(mixins), mixins_2_1 = mixins_2.next(); !mixins_2_1.done; mixins_2_1 = mixins_2.next()) { var mixin_1 = mixins_2_1.value; copyProperties(this, new mixin_1()); // Copy Instance Properties } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (mixins_2_1 && !mixins_2_1.done && (_a = mixins_2.return)) _a.call(mixins_2); } finally { if (e_2) throw e_2.error; } } } return Mixin; }()); try { for (var mixins_1 = __values(mixins), mixins_1_1 = mixins_1.next(); !mixins_1_1.done; mixins_1_1 = mixins_1.next()) { var mixin_2 = mixins_1_1.value; copyProperties(Mixin, mixin_2); // Copy static properties copyProperties(Mixin.prototype, mixin_2.prototype); // Copy prototype properties } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (mixins_1_1 && !mixins_1_1.done && (_a = mixins_1.return)) _a.call(mixins_1); } finally { if (e_1) throw e_1.error; } } return Mixin; }; export default mixin; //# sourceMappingURL=mixin.js.map