在 ClojureScript 中实现静态属性

Implementing static properties in ClojureScript

如何在 ClojureScript 中模仿这种 JavaScript 继承?

class AccessController extends EventEmitter {

  static async create (db, options) { }

  static get type () {
    throw new Error('\'static get type ()\' needs to be defined in the inheriting class')
  }

  get type () {
    return this.constructor.type
  }

  async canAppend (entry, identityProvider) {
    return true
  }

}

class OtherController extends AccessController {

 constructor (db, options) {
    super()
  }

  static async create (db, options) {
    return new OtherController (db, options)
  }

  static get type () {
    return 'othertype'
  }

  async canAppend(entry, identityProvider) {
    return true
  }
}

Mu的理解是:

这将是这样实现的:

(defn- OtherController
  {:jsdoc ["@constructor"]}
  [orbitdb options]
  (this-as this
    (.call AccessController this orbitdb options)
    this))

(defn create-access-controller []
  (gobj/extend
      ;; inheritance
      (.-prototype OtherController)
      (.-prototype AccessController)

    ;; methods
    #js {:canAppend (fn [entry identity-provider]                          
                      true)})

    ;; static properties
    (set! (.. OtherController -type) "othertype")

    (set! (.. OtherController -create) (fn [db options]
                                         (new OtherController db (clj->js {}))))
  OtherController)

我不确定如何:

  • get 您可以通过 Object.defineProperty.
  • 创建 我认为
  • static 属性通常不会被继承,但您可以对 类 本身而不是它们的原型进行同样的 gobj/extend 调用。