1

I want to instantiate a class in javascript, pass the object over to my ClojureScript, and access its properties and functions from Clojure. How can I access the functions and properties of test-obj when I only have clojure.core namespace at my disposal?

I have the following awful hacked solution.

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:

(defn myfn[] 
 (let [test-obj (.createTest js/window "x" 1)]
 [:div (.geta js/window test-obj)]))
asked Mar 5, 2021 at 20:28

1 Answer 1

2

Regular JS interop should work just fine I believe :)

Expose your class to the outside world:

window["Test"] = Test

Then, from cljs:

(let [x (js/Test.)]
 (prn (.getA x)))
answered Mar 6, 2021 at 11:57
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.