ClojureScript Gotchas & Tips
I’ve been playing a lot lately with ClojureScript, especially with regards to mobile development (more on that to come later). Clojure has never been known for great error messages, and ClojureScript is probably even worse in that regard.
Included below are a list of things that gave me a rough time. Some are pretty simple, but needless to say they got me. Hopefully they won’t get you.
The ns function is very special in ClojureScript when compared to Clojure. I won’t go over all the differences, those have been covered, but I’ll highlight a few things quickly: only one namespace per file; use requires only and require requires as; REPL requires load-namespace instead of ns call; invalid require statements don’t give compile errors.
A big one that got me is requiring/using the Google Closure libs and classes. When you require them in your namespace, the ClojureScript compiler just outputs
:as makes no difference here. When you refer to the functions/objects, you have to use the fully qualified name, e.g.
To sum that annoying issue up, you have to both require and use the fully-qualified name.
Interop With JS Objects
You can also use the goog.object package for help which is wrapped by several CLJS functions, the primary one being js-obj which takes arguments and simply applies them to goog.object/create.
Arity With Functions
The Compiler Doesn’t Care
Don’t expect the compiler to help you out chasing and picking out errors. The compiler only ensures the code can be read and checks for a few minor things (like :require has :as, etc). The vast majority of errors will be found at runtime. You’ll have to change your mindset from compiled language to interpreted language.
Himera ClojureScript REPL - An online REPL. Very useful! Don’t miss the Summary page.
(= :ClojureScript :Clojure). They are different.
Surf the source!
When you get a “call undefined” error, you most likely forgot your require, misspelled it, or similar.
You could use the console to probe and find the culprit, e.g. type
Keep probing the environment to see what is not as is expected.⬅ Go back