Skip to content

Instantly share code, notes, and snippets.

Last active September 16, 2021 07:26
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Notes on Clojurescript
; Clojure strings are Javascript strings
(def s "This is a string")
; Can access Javascript functions, using dot-prefixed notation
(.toUpperCase s)
; To retrieve an object property
(.-length s)
; Javascript supports method-chaining. To accomplish the same in Clojurescript we would nest the function calls, or better still
; we could use the threading macros.
; Alternatively, Clojurescript has a double-dot notation
(.. s (charCodeAt 7) (toString 16) (toUpperCase))
; Property getters and setters
(set! js/newvar 3)
; Javascript objects are created
(js-obj "x" 1 "y" 2)
; Javascript arrays are created
(def arr (array 1 2 3))
; Access properties
(aset js-obj "z" 1)
(aget arr 1) ;=> 2
; Javascript globals are accessed using the js namespace
; Javascript constructors i.e. new Date() - by placing a dot after the function
(def tmp (js/NameOfClass. "arg1" "arg2"))
; Convert Clojurescript maps to Javascript objects using #js reader macro. Only converts top level
#js {:map {:ima "map"}
:object #js {:ima "js-obj"}}
; To map between nested Javascript and Clojurescript use js->clj, and clj->js
; References
; 1.
; 2.
; 3.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment