When I learned and used ClojureScript, I really hated writing code that looks like this:

(defn Bag [] (this-as this (set! (.-store this) (array)) this)) (set! (.. Bag -prototype -add) (fn [val] (this-as this (.push (.-store this) val)))) (set! (.. Bag -prototype -print) (fn [] (this-as this (.log js/console (.-store this))))) (def mybag (Bag.)) (.add mybag 5) (.add mybag 7) (.print mybag)

That’s so much ceremony and repeated waste!

I know it’s not how you’re supposed to write ClojureScript, but sometimes you have to (for example when working with OO libraries).

What do you do with such code? Cover it with macros!

So I wrote two little macros that you can just pick up and use right away in your ClojureScript, so that it can look like:

(defn Bag [] (this-as this (set! (.-store this) (array)) this)) (set-obj-fn Bag.prototype.add [val] (.push (.-store this) val)) (set-obj-fn Bag.prototype.print [val] (.log js/console (.-store this)))

Or just a this-as shortcut:

(set! (.. Bag -prototype -add) (obj-fn [val] (.push (.-store this) val)))