简体   繁体   English

如何从Clojurescript调用Javascript?

[英]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. 我正在学习clojure,我正在尝试将一些javascript从CodeCombat转换为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. 我试图将Javascript代码转换为Clojure,但不幸的是CodeCombat没有给我任何错误消息。

(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 我主要关注官方互操作教程http://clojure.org/java_interop

Use this-as macro ! 使用此作为宏! However, using def is not nice inside macro... it's preferred to use let if possible! 但是,在宏内部使用def并不好...如果可能的话,最好使用let

(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). 在你的代码中删除base周围的括号,(它是函数调用,你不想调用它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM