Anton Harald
Anton Harald

Reputation: 5934

inspect by 'new' constructed JavaScript object's at the ClojureScript REPL

When I access the following JS object at the ClojureScript REPL I receive this beauty:

#object[Url [object Object]]

How can I inspect it / see what is inside / convert it to a Clojure data structure?


I figured out, that the Clojure REPL only remains as quiet about the object's content when the object was created by a constructor function with JavaScript's new keyword. (However built-in constructors like Array seem to be an exception)

cljs.user=> #js {:a "x"}
#js {:a "x"}

cljs.user=> (defn A [] (this-as my-this (set! (.-a my-this) "x")))
#'cljs.user/A
cljs.user=> (A.)
#object[cljs$user$A [object Object]]

This confused me, since - as mentioned in an answer - the browser's REPL (at least chromium and firefox) does print out the content of these objects. Also the Node.js REPL does. The function js->clj is not helping here. As the code above shows, the Clojure REPL prints even unconverted JavaScript Objects's contents.

Until now I could not find a way to inspect the content as a whole of such objects. I can only access the properties separately. e.g:

cljs.user=> (.-a (A.))
"x"

Has by any chance anybody bumped into the same problem and maybe even found a solution or explanation for this?

Upvotes: 3

Views: 533

Answers (3)

Anton Harald
Anton Harald

Reputation: 5934

The problem was to cast from #object[Url "..."] to #object[Object "..."] Finally I did it with this good old helper function.

(defn clone-js [jsobj]
  (.parse js/JSON (.stringify js/JSON jsobj)))

I'd still appreciate a lot to hear if there are proper solution out there.

Upvotes: 1

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276286

Use js->clj :

(def myobj (js->clj yourJsObj))

See this list for more interop examples.

Typically, js->clj and clj->js are helpful for interoping.

Upvotes: 0

user4554100
user4554100

Reputation:

Log it in the console and look with chrome/firefox

Upvotes: 0

Related Questions