[英]How can I use named parameters in ClojureScript?
In clojure I can use defnk to get named parameters. 在clojure中,我可以使用defnk来获取命名参数。 How can I achieve the same thing in ClojureScript? 如何在ClojureScript中实现相同的功能?
The named args functionality in ClojureScript is the same as in Clojure: ClojureScript中命名的args功能与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
If you want default values, then change the original to: 如果您需要默认值,请将原始内容更改为:
(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
This is related to the nice answer for Clojure - named arguments 这与Clojure的好答案有关- 命名参数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.