Search code examples
javascriptclojureclojurescript

How do I call Javascript from Clojurescript?


I am currently learning clojure and I am trying to translate some javascript from CodeCombat to clojure/clojurescript.

var base = this;
var items = base.getItems();
if (base.built.length === 0)
    base.build('peasant');

I am trying to convert the Javascript code to Clojure, but unfortunately CodeCombat doesn't give me any error message.

(def base this)
(def items (.getItems (base) ))
(def built-len ((.length) (.built (base)) ))  
(if (= built-len 0)
    ((.build "peasant") (base) )))

Do you see any obvious mistake? I mostly followed the offical interop tutorial http://clojure.org/java_interop


Solution

  • Use this-as macro ! However, using def is not nice inside macro... it's preferred to use let if possible!

    (this-as t
      (let [item (.getItems t)]
    

    In your code remove parenthesis around base, (it's function call, and you don't want to call it).