Last active
September 16, 2021 07:26
-
-
Save pjullah/b947e85081b8d3431d0cfb8e906fed20 to your computer and use it in GitHub Desktop.
Notes on Clojurescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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 | |
(js/xxx) | |
; Javascript constructors i.e. new Date() - by placing a dot after the function | |
(js/Date.) | |
(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. http://www.spacjer.com/blog/2014/09/12/clojurescript-javascript-interop/ | |
; 2. https://www.reddit.com/r/Clojure/comments/5ec8kr/lumo_nodebased_clojurescript_repl_has_a_100/ | |
; 3. http://clojurescriptmadeeasy.com/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment