在 clojure 中,我可以使用 defnk 来获取命名参数。如何在 ClojureScript 中实现相同的目标?
问问题
1183 次
1 回答
11
ClojureScript 中的命名参数功能与 Clojure 中的相同:
(defn f [x & {:keys [a b]}]
(println (str "a is " a " and b is " b)))
(f 1)
; a is and b is
(f 1 :a 42)
; a is 42 and b is
(f 1 :a 42 :b 108)
; a is 42 and b is 108
如果您想要默认值,则将原始值更改为:
(defn f [x & {:keys [a b] :or {a 999 b 9}}]
(println (str "a is " a " and b is " b)))
(f 1)
; a is 999 and b is 9
于 2012-01-03T16:42:37.137 回答