This is a note of the Learn ClojureScript.
ClojureScript and Tools
Clojure was first released in 2007 and ClojureScript was first released in 2011. They are largely the same language running on different platforms. It is practical, enjoyable, terse and has more libs than JavaScript. The best part for ClojureScript is its comprehensive ecosystem that includes FP, libs, modules, tools, tests and builds. It has almost everything required for efficient web development.
The immutable data structure and FP optimize UI. ClojureScript uses CSP (Communicating Sequential Processes) for async tasks.
Figwheel compiles and load changes into the browser instantly. There are three pillars of reloadable code
- idempotent functions
defonce
: it binds the var once, as well as evaluation of its expression once.- separate UI and business logic.
Interacting with JavaScript and DOM
Use js->clj
for converting from JS data and clj->js
for converting to JS. ClojureScript creates a namespace js
that contains all of the global JS variables. Use set!
to update a js variable.
Use js-obj
or #js
to create object literal. Use aget
and aset
to get and set properties. Properties can be accessed with .-propertyName obj
and seet with set! (.-propertyName obj) value
.
use (.methodName obj args)
to call a JS object method.
We use Google Closure’s DOM lib goog.dom
to manipulate DOM. The root is js/document
. Google dom lib has functions such as createElement
, setTextContent
, appendChild
, setProperties
, getElement
, etc. Use aget
to get an element’s value.
goog.events
has listen
to bind event handler.