如何在 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
}
}
穆的理解是:
static
是对象本身的属性- 原型属性(方法)可以通过设置
-.prototype
这将是这样实现的:
(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
糖, - 因为
AccessController extends EventEmitter
我如何继承 的static
属性(EventEmitter
如果有的话)?