简体   繁体   English

从 ClojureScript 调用 javascript Object

[英]calling javascript Object from ClojureScript

I want to instantiate a class in javascript, pass the object over to my ClojureScript, and access its properties and functions from Clojure.我想在 javascript 中实例化一个 class,将 object 传递给我的 ClojureScript,并从 Z3CE402Z34C404314EF19DEE 访问它的属性和函数How can I access the functions and properties of test-obj when I only have clojure.core namespace at my disposal?当我只有 clojure.core 命名空间可供我使用时,如何访问 test-obj 的功能和属性?

I have the following awful hacked solution.我有以下可怕的黑客解决方案。

Javascript: Javascript:

class Test {
  constructor (a,b) {
    this.a = a;
    this.b = b;
  }
  getA () {
    return this.a;
  }
}

window['createA'] = (a,b) => {
  return new Test(a,b);
}

window['geta'] = (o) => {
  return o.getA();
}

ClojureScript: ClojureScript:

(defn myfn[] 
  (let [test-obj (.createTest js/window "x" 1)]
  [:div (.geta js/window test-obj)]))

Regular JS interop should work just fine I believe:)我相信常规的 JS 互操作应该可以正常工作:)

Expose your class to the outside world:将您的 class 暴露给外界:

window["Test"] = Test

Then, from cljs:然后,从 cljs:

(let [x (js/Test.)]
  (prn (.getA x)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM