function/macro | since v0.0-927 | Edit |
(array var-args)
(array 1 2 3 ...)
Creates a JavaScript array containing the given args.
The tagged literal #js [1 2 3]
is equivalent to (array 1 2 3)
(array 1 2 3)
;;=> #js [1 2 3]
(apply array [1 2 3])
;;=> #js [1 2 3]
#js [1 2 3]
;;=> #js [1 2 3]
When creating nested JavaScript arrays, you can opt to use clj->js
instead:
(array 1 2 (array 3 4))
;;=> #js [1 2 #js [3 4]]
(clj->js [1 2 [3 4]])
;;=> #js [1 2 #js [3 4]]
Creates a new javascript array. @param {...*} var_args
(defn ^array array
;;array is a special case, don't emulate this doc string
[var-args] ;; [& items]
(let [a (js/Array. (alength (cljs.core/js-arguments)))]
(loop [i 0]
(if (< i (alength a))
(do
(aset a i (aget (cljs.core/js-arguments) i))
(recur (inc i)))
a))))
(core/defmacro array [& rest]
(core/let [xs-str (core/->> (repeat "~{}")
(take (count rest))
(interpose ",")
(apply core/str))]
(vary-meta
(list* 'js* (core/str "[" xs-str "]") rest)
assoc :tag 'array)))