-
Notifications
You must be signed in to change notification settings - Fork 8
Features
ClojureJS implements let
and loop/recur
with anonymous functions in JavaScript to ensure consistent scoping rules. See the Examples section for samples of let
expansions.
ClojureJS tries to be exhaustive about implementing implicit return in the generated JavaScript. This is one area where more unit tests are much needed.
The following reserved forms are implemented by the ClojureJS translator:
count, def, defn, defmacro, do, dokeys, fn, get, if, inline, let, loop,
new, nil, quote, recur, return, set!, throw, try/catch/finally
The translator rewrites Lisp style symbols like *foo*
, number?
, and inc!
to acceptable JavaScript forms such as the following:
Lisp JavaScript
---- ----------
foo-bar foo_bar
*foo* _foo_
number? numberp
inc! incf
and &&
or ||
However, quoted and keyword symbols are not name mangled as of ClojureJS 1.2.7, e.g.,
(js {:foo-bar 1 'list-action 2})
now generates:
"{'foo-bar' : 1,'list-action' : 2}"
ClojureJS recognizes the following standard JavaScript operators:
++ -- !
&& || + - * / %
> >= < <= == === != !==
instanceof
JavaScript Array and Object member access is via the get
form, e.g.,
(def arr [:foo :bar :baz])
(get arr 0) ;; => :foo
(set! (get arr 0) :quux)
arr ;; => [:quux :bar :baz]
;; set the fillStyle of a canvas context
(set! (get ctx 'fillStyle) "red")
;; or alternatively,
(set! (:fillStyle ctx) "red")
ClojureJS introduces a couple of special forms, to support JavaScript specific functionality.
dokeys
is a Clojure (subset) equivalent of the JavaScript for..in
loop.
(dokeys [k attrs] (.setAttribute el k (get attrs k)))
translates to the following JavaScript:
for (var k in attrs) { el.setAttribute(k, attrs[k]); }
inline
is an escape hatch to introduce inlined JavaScript code, e.g,
(defn isa? [i c] (inline "i instanceof c"))
translates to the following JavaScript:
isap = function(i, c) { return i instanceof c; }