r/Clojurescript • • 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

5 comments sorted by

View all comments

Show parent comment

1

u/_woj_ Oct 31 '17

Thanks, good point. I changed the function name and received an error. I remember Rich Hickey saying that "order of everything matters so much" in imperative languages, but order matters in Clojure as well. Or it is just that even though the order still matters, Clojure is more about working with blocks of logic rather than lines of code?

2

u/grav Nov 01 '17

Order probably matters less in functional languages in general. But Clojure is also a very dynamic language, which has its pros and cons.

defs are a kind of mutable state of your namespace, and mutable state always messes stuff up. Having as little as possible, and understanding the nature of the little you have, is the advice I'd give :-)