如何在JavaScript中模仿ClojureScript继承?
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,我如何继承EventEmitter的static属性(如果有的话)?发布于 2020-05-19 13:59:40
get您可以通过Object.defineProperty创建。static属性通常不是继承的,但您可能只需对类本身执行相同的gobj/extend调用,而不是对它们的原型执行相同的gobj/extend调用。https://stackoverflow.com/questions/61891349
复制相似问题