Protocols – Extend the protocol in CLOJUREScript to get values ​​from this machine JS object

I have a code base that uses a lot of get and get-in for nested forms. I want to be able to use native javascript objects without (a lot of) code rewriting.

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 do not modify them, I think it is enough to implement get (get-in dependency). This 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 breaks a lot of things in a strange way.

You are modifying the Object prototype, you don’t want to do this, the following is better:

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

I have a code base that uses a lot of get and get-in for nested forms. I want to be able to use native javascript objects without (a lot of) code rewriting .

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 think it is enough to implement get (get-in dependency). This is my attempt ,

< p>

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

It seems to work, but it breaks a lot of things in a strange way.

< p>You are modifying the Object prototype and you don’t want to do this. The following is better:

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

Leave a Comment

Your email address will not be published.