Skip to content
This repository has been archived by the owner on Mar 14, 2021. It is now read-only.

Features

kriyative edited this page Jul 12, 2011 · 1 revision

Scoping Rules

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.

Implicit Return

ClojureJS tries to be exhaustive about implementing implicit return in the generated JavaScript. This is one area where more unit tests are much needed.

Reserved Symbols/Forms

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

Symbol Translation

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}"

Operators

ClojureJS recognizes the following standard JavaScript operators:

++ -- !
&& || + - * / %
> >= < <= == === != !==
instanceof

Arrays and Objects

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")

Special Forms

ClojureJS introduces a couple of special forms, to support JavaScript specific functionality.

dokeys

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

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; }
Clone this wiki locally