Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>After lots of trial and error, I figured out how to have the same structure for clojurescript as for javascript.</p> <p>The <code>this-as</code> macro has a few idiosyncrasies and only works when the method is put into the class</p> <p>for example I want to create something that looks like this in javascript:</p> <pre><code>var anobj = {a: 9, get_a: function(){return this.a;}}; </code></pre> <p>I have to do a whole lot more coding to get the same object in clojurescript:</p> <pre><code>(def anobj (js-obj)) (def get_a (fn [] (this-as me (.-a me)))) (aset anobj "a" 9) (aset anobj "get_a" get_a) </code></pre> <p>which is seriously ugly for a language as beautiful as clojure. Things get worse when you have got functions that link to each other, like what happens in knockout. </p> <p>I found that the best way to create an js-object with lots of <code>this</code>'s in there is to define a <code>__init__</code> method, add it to the class and then run it, then remove it from the class. For example, if I wanted to make another object: </p> <pre><code>var avobj = {a: this, b: 98, c: this.a get_a: function(){return str(this.a) + str(this.c);}}; </code></pre> <p>written as clojurescript with and <code>__init__</code> method looks like this:</p> <pre><code>(def avobj (js-obj)) (def av__init__ #(this-as this (aset this "a" this) (aset this "b" 9) (aset this "c" (.-a this)) (aset this "get_a" (fn [] (str (.-a this) (.-c this)))))) (aset avobj "__init__" av__init__) (. avobj __init__) (js-delete stuff "__init__") </code></pre> <p>There's still a whole bunch more code than javascript... but the most important thing is that you get the same object as javascript. Setting all the variables using this form also allows the use of macros to simplify. So now I have defined a macro:</p> <pre><code>(defmacro defvar [name &amp; body] (list 'do (list 'def name (list 'map-&gt;js { :__init__ (list 'fn [] (list 'this-as 'this (list 'aset 'this "a" "blah"))) })) ;(. js/console log ~name) (list '. name '__init__) (list 'js-delete name "__init__"))) </code></pre> <p>and with map->js taken from jayq.utils:</p> <pre><code>(defn map-&gt;js [m] (let [out (js-obj)] (doseq [[k v] m] (aset out (name k) v)) out)) </code></pre> <p>Now I can write code like this:</p> <pre><code>(defvar avobj a this b 9 c (.-a this) get_a (fn [] (str (.-a this) (.-c this)))) </code></pre> <p>and for the answer to knockout:</p> <pre><code>(defvar name_model first_name (observable "My") last_name (observable "Name") name (computed (fn [] (str (. this first_name) " " (. this last_name))))) (. js/ko (applyBindings name_model)); </code></pre> <p>Which is really nice for me as it matches javascript really well and its entirely readable!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload