如何在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
,如果有的话?get
你可以通过 对象.defineProperty.static
我想,这些属性通常不会被继承,但你也可以做同样的事情。gobj/extend
调用类本身,而不是它们的原型。