Extend protocol in ClojureScript to get value from embedded js object

I have a codebase that heavily uses get and get-in for nested forms. I want to be able to use my own javascript objects without rewriting (large) code.

 js> cljs.user.o = {foo: 42} // in js console cljs.user> (get o "foo") ; => 42 ; in cljs console 

Since I only request forms, but don’t change them, I thought this would be enough to implement get (which get-in relies on). Here is my attempt

 (extend-protocol ILookup js/Object (-lookup [mk] (aget mk)) (-lookup [mk not-found (or (aget mk) not-found))) 

It seems to work, but it strangely breaks a lot of things.

+4
source share
1 answer

You change the prototype of the object, you do not want to do this, it is better:

 (extend-protocol ILookup object (-lookup [mk] (aget mk)) (-lookup [mk not-found] (or (aget mk) not-found))) 
+9
source

Source: https://habr.com/ru/post/1484172/


All Articles