ClojureScript interop

后端 未结 2 593
一整个雨季
一整个雨季 2021-02-05 16:27

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

2条回答
  •  独厮守ぢ
    2021-02-05 17:00

    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
    

提交回复
热议问题