我正在尝试使用特定的 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 不是协议(protocol)”。另外,我不想添加方法,而是创建一个继承某些方法并重载其他方法的子类。
我考虑过使用proxy
,但是“core/proxy is not defined”。
当然,我可以创建一个 Bar
实例和 set!
新方法,但这感觉就像放弃并使用劣等语言。
最佳答案
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
))})
关于clojurescript - 语言脚本 : extend a Javascript class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61040644/