ClojureScript中的js / console.log [英] js/console.log in ClojureScript
问题描述
我想用ClojureScript实现一个函数,以简化 js / console.log
,如下所示:
I want to implement a function with ClojureScript to simplify js/console.log
like this:
(defn log [& args]
(apply js/console.log args))
调用它:(logfoobar)
throws: TypeError:非法调用
但这样做有效:(js / console.logfoobar)
有什么问题?
推荐答案
js / something
是用于访问js对象,但是你不应该嵌套点,因为它不是clojure兼容的语法,它将被删除。在旧版本的编译器(2138)中,您的代码可以工作,但是在较新版本的编译器中可能已被弃用。你使用哪个版本?
js/something
is for accessing a js object, but you shouldnt nest dots after that, since it is not clojure compatible syntax and it was going to be removed. In older versions of the compiler (2138) your code works, but it is possible that it has been deprecated in newer versions of the compiler. Which version are you using?
正确的方法是使用这样简单的js interop:[注意:见下面的评论,来自ClojureScript首席开发人员David Nolen]
The correct way would be using straightforward js interop like this: [Caveat: see comment below from David Nolen, ClojureScript lead developer]
(defn log [& args] (apply (.-log js/console) args))
甚至更短,因为console.log已经是可变的(只是做一个别名):
And even shorter, since console.log is already variadic (just make an alias):
(def log (.-log js/console))
这篇关于ClojureScript中的js / console.log的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!