5

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.

1 Answer 1

10

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)))
Sign up to request clarification or add additional context in comments.

1 Comment

@tomconnors No need to add text for reviewers to remove; you can just add some  s

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.