这是 ClojureScript 的字符串“替换”函数的原始代码块:
(defn replace
"Replaces all instance of match with replacement in s.
match/replacement can be:
string / string
pattern / (string or function of match)."
[s match replacement]
(cond
(string? match)
(.replace s (js/RegExp. (gstring/regExpEscape match) "g") replacement)
(instance? js/RegExp match)
(if (string? replacement)
(replace-all s match replacement)
(replace-all s match (replace-with replacement)))
:else (throw (str "Invalid match arg: " match))))
正如你在这一行看到的:
[s match replacement]
,此方法接受三个参数。来自我的 REPL:
user=> (replace ":c41120" ":" "")
ArityException Wrong number of args (3) passed to: core/replace clojure.lang.AFn.throwArity (AFn.java:429)
我是唯一一个认为我通过了正确数量的参数 (3) 的人吗?知道为什么这会失败吗?
问题,第二部分:具体
在我的 components.cljs 文件中,我有这些“要求”:
(ns labrador.components
(:require [re-frame.core :as rf]
[reagent.core :refer [atom]]
[clojure.string :as s]
[labrador.helpers :as h]))
我已经成功使用“s/join”和“s/blank?”在这个文件中。但是当我尝试使用下面的“s/replace”时(注意“replace”调用在第 484 行):
(for [roll-count order-item-roll-counts]
(let [key (key roll-count)
val (val roll-count)
code (s/replace key ":" "")]
...我收到以下错误:
Uncaught TypeError: s.replace is not a function
at clojure$string$replace (string.cljs?rel=1489020198332:48)
at components.cljs?rel=1489505254528:484
...当我显式调用替换函数时,如下所示:
code (clojure.string/replace key ":" "")]
...我仍然得到完全相同的错误,就好像我仍在调用“s/replace”一样。
我是 Clojure/ClojureScript 的新手,我明显无知。
最佳答案
首先,看起来您是在 Clojure REPL 中运行,而不是 ClojureScript,其次,您正在调用 clojure.core/replace , 而不是 clojure.string/replace .
关于replace - 无法解决 clojure.string/replace "is not a function"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42775272/