0

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

Maik Klein
  • 15,548
  • 27
  • 101
  • 197
  • 1
    There are many differences between Clojure and ClojureScript, so I suggest you focus on one. The ClojureScript documentation shows how to do JS interop. https://github.com/clojure/clojurescript/wiki/Differences-from-Clojure#host-interop – Jeremy May 20 '14 at 20:47
  • 1
    See this question on how to access `this` from ClojureScript http://stackoverflow.com/questions/15531261/accessing-this-in-clojurescript – Diego Basch May 20 '14 at 21:33
  • Remember that putting parens around something tries to call the first item in the list as a function. Are you sure you want to call the string "peasant" as a function? – Chuck May 21 '14 at 00:11
  • I am currently learning clojure and clojure script too. And maybe it would help you. This is how I do what you're talking about and it works for me `(defn get-url [path] (.getURL js/chrome.extension path))` – Igor Tyulkanov May 21 '14 at 04:56

1 Answers1

2

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).

user8472
  • 3,268
  • 3
  • 35
  • 62
markus
  • 1
  • 1