Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClojureScript interop

I am trying to find out how to access Javascript objects properties in ClojureScript. If I know in advance the name of the property, that is easy. To get foo.bar I just do

(.-bar foo)

Is there a way to access a property whose name is known only at runtime? I am looking for the equivalent of the JS syntax foo[dynamicBar]

like image 855
Andrea Avatar asked Mar 25 '12 15:03

Andrea


1 Answers

You can use aget / aset to access properties known only at runtime.

;; Use clj->js to convert clj(s) map to javascript.
;; Note the #js {:bar 100} reader literal indicating a js map.

cljs.user> (def foo (clj->js {:bar 100}))
#js {:bar 100}
cljs.user> (.-bar foo) 
100
cljs.user> (aget foo "bar")
100
cljs.user> (aset foo "baz" 200)
200
cljs.user> (.-baz foo) 
200
like image 74
sw1nn Avatar answered Sep 26 '22 07:09

sw1nn