2013-11-22 35 views
2

我知道,你可以使用下面的types延長擴展協議JS日期clojurescript

object, array, function, string, nil 

(extend-type nil Functor 
(fmap 
    ([_ _] nil) 
    ([_ _ _] nil))) 

我希望爲本地日期對象做同樣的。這是如何完成的?

也..有沒有更多的小寫字母,我錯過了?

回答

4

下面是如何延長日期對象:

(defprotocol Functor 
    (fmap [_])) 

(extend-type js/Date 
    Functor 
    (fmap 
    ([_] (.log js/console 42)))) 

(fmap (js/Date.))  ;; logs 42 

小寫類型的列表(來自https://github.com/clojure/clojurescript/blob/202cfcf045cf86d3ab295cbf16a347569b652647/src/cljs/clojure/data.cljs):

零,字符串,數字陣列,功能,布爾值,默認

來自himera(http://himera.herokuapp.com/synonym.html):

;; In addition native JavaScript objects like 
;; Function, Object, Array, Number, String 
;; are never actually directly extended 

;; For example say you'd like to use RegExps 
;; as functions 

(extend-type js/RegExp 
    IFn 
    (-invoke 
    ([this s] 
    (re-matches this s)))) 

(filter #"foo.*" ["foo" "bar" "foobar"]) 
;; => ("foo" "foobar") 
;; This is precisely how callable collections 
;; are implemented.