This writes to console the way I would expect:
(.log js/console "hi" "there")
The output is
hi there
This, however, just writes a big mess to the console:
(defn log-it [& args] (.log js/console args))
(log-it "hello" "there")
The output is:
c…s.c…e.IndexedSeq {arr: Array[2], i: 0, meta: null, cljs$lang$protocol_mask$partition0$: 166592766, cljs$lang$protocol_mask$partition1$: 8192}
This also does NOT work:
(apply .log js/console ["hi" "there"])
Is there a way to pass the vector's elements into the .log function?
Do I have to write a macro to concat the vector on to '(.log js/console)?
The problem here is that you're trying to log the value of
args(which is a Clojure IndexedSeq) rather than passing the values in the seq as individual arguments. In this case you need to useapply(or convert that sequence to a native data structure).The reason your other example doesn't work should become clear if you look at the signature for
apply.It expects the first argument to be the function you want to invoke, but here, the first argument is
.log.Remember that
.log js/consoleis the syntax for invoking thelogmethod onconsole. Instead, we want to get a reference to theconsole.logfunction.We're using
.-logto read the.logproperty, rather than call it as a method. Then we pass that along with ourargssequence.So instead, you can define the original function as:
Or if you want to get a little bit smarter.