I have a codebase that makes a heavy use of get
and get-in
for nested forms. I want to be able to use native javascript objects too, without (much) code rewrite.
js> cljs.user.o = {foo: 42} // in js console
cljs.user> (get o "foo") ; => 42 ; in cljs console
Since I only query the forms, but don't modify them, I thought it would be enough to implement get
(which get-in
relies on). Here is my attempt,
(extend-protocol ILookup
js/Object
(-lookup [m k] (aget m k))
(-lookup [m k not-found (or (aget m k) not-found)))
It seems to work, but it breaks a lot of things in a strange way.
You're modifying the Object prototype, you don't want to do that, the following is better:
(extend-protocol ILookup
object
(-lookup [m k] (aget m k))
(-lookup [m k not-found] (or (aget m k) not-found)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With