1

我正在尝试使用一个特定的 JavaScript 框架,该框架需要扩展一个基类才能将其用于应用程序。

基本上,我想以惯用的 ClojureScript 执行以下操作。

class Foo extends Bar {
  constructor() { super("data") }
  method1(args) { /* do stuff */ }
}

我试过了

(defn foo
  []
  (reify
    js/Bar
    (constructor [this] (super this "data"))
    (method1 [this args] )))

如果我从 Object 创建一个新类,这会起作用,但正如shadow-cljs正确抱怨的那样,“Symbol js/Bar 不是协议”。另外,我不想添加方法,而是创建一个继承某些方法并重载其他方法的子类。

我考虑过使用proxy,但“未定义核心/代理”。

当然,我可以创建一个实例Barset!新方法,但这感觉就像放弃并使用低级语言。

4

1 回答 1

8

CLJS 没有对class ... extends ....

你可以自己用一些样板来破解它,你可以通过宏生成它以使其看起来很漂亮。

(ns your.app
  (:require
    [goog.object :as gobj]
    ["something" :refer (Bar)]))

(defn Foo
  {:jsdoc ["@constructor"]}
  []
  (this-as this
    (.call Bar this "data")
    ;; other constructor work
    this))

(gobj/extend
  (.-prototype Foo)
  (.-prototype Bar)
  ;; defining x.foo(arg) method
  #js {:foo (fn [arg]
              (this-as this
                ;; this is Foo instance
                ))})
于 2020-04-05T10:05:55.393 回答