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

asked May 20, 2014 at 20:31
4
  • 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. github.com/clojure/clojurescript/wiki/… Commented May 20, 2014 at 20:47
  • 1
    See this question on how to access this from ClojureScript stackoverflow.com/questions/15531261/… Commented May 20, 2014 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? Commented May 21, 2014 at 0: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)) Commented May 21, 2014 at 4:56

1 Answer 1

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,3464 gold badges38 silver badges65 bronze badges
answered May 21, 2014 at 8:59
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.