View on GitHub

Cycle263 Blog

Stay hungry, stay foolish.

ES6 类

Mixin模式的实现

Mixin模式指的是,将多个类的接口“混入”(mix in)另一个类。

  function mix(...mixins) {
    class Mix {}

    for (let mixin of mixins) {
      copyProperties(Mix, mixin);
      copyProperties(Mix.prototype, mixin.prototype);
    }

    return Mix;
  }

  function copyProperties(target, source) {
    for (let key of Reflect.ownKeys(source)) {
      if ( key !== "constructor" && key !== "prototype" && key !== "name" ) {
        let desc = Object.getOwnPropertyDescriptor(source, key);
        Object.defineProperty(target, key, desc);
      }
    }
  }

  // 使用
  class DistributedEdit extends mix(Loggable, Serializable) { }