r/Clojurescript • u/_woj_ • Oct 28 '17
Function Hoisting in ClojureScript
I'm relatively new to Clojure/ClojureScript, and I was playing around in this online ClojureScript editor (http://app.klipse.tech/) when I realized that this code worked and printed to the console:
(myFunc)
(defn myFunc [] (println "hello hello"))
I'm just wondering why it knows what myFunc is before it's defined. Is is a part of Clojure or a part of JavaScript? Would this code work in JVM clojure?
5
Upvotes
View all comments
Show parent comment
1
u/grav Oct 28 '17 edited Oct 28 '17
Yes, this can be confusing. I often iterate by hacking away in a namespace, and then sending it to a REPL. If the code looks like this in first iteration:
... and then in the next iteration looks like this:
... I will see 42 on the screen, since it's the old version of
foothat's evaluated.And if I run the code in another context, I'll get an error such as
Cannot read property 'call' of undefined.This can really be annoying, and very error prone, if you for instance rename a function, and code is still using the old name (and this version) of the function. I often just use
(remove-ns 'whatever)in the REPL to clean up.Stuart Sierra has an article about mitigating this (among many other) problem with his workflow called "Reloaded": http://thinkrelevance.com/blog/2013/06/04/clojure-workflow-reloaded