Clojurscript:扩展Javascript类

Waechtertroll

我正在尝试使用特定的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!方法的实例,但是这就像放弃和使用劣等语言一样。

托马斯·海勒

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
                ))})

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章