Extending a protocol to one of the global JS objects is bad for the same reason 
it is bad in Javascript: you are modifying a global object. Your 
extend-protocol is like saying Function.prototype.FOO_foo = function(this, 
arg){...} in javascript. (The "FOO_foo" is a long namespaced and mangled 
property name, but you are still modifying the global.)

You can extend "function" (and "object", "number", "string", "array", and 
"boolean") instead of js/Function (or js/Object, etc). This will implement the 
protocol from the "outside" (doing type checks on its argument) instead of 
assigning a new member to the global's prototype. E.g.:

(extend-protocol Foo
  function
  (foo [this] (println this)))

I'm not sure if you also need to extend IFn: I think "function" means only 
native js functions and not clojurescript objects implementing IFn. (Test with 
a multi-arity cljs function to verify.)


You can also extend the "default" type, which means everything will implement 
the protocol that doesn't have a more specific implementation.

I don't know where these magic protocol type names are documented.

On Thursday, September 3, 2015 at 6:58:48 AM UTC-5, Scott Nelson wrote:
> (defprotocol Foo
>   (foo [arg]))
> 
> (extend-protocol Foo
>   js/Function
>   (foo [arg]
>     (println arg)))

-- 
Note that posts from new members are moderated - please be patient with your 
first post.
--- 
You received this message because you are subscribed to the Google Groups 
"ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojurescript+unsubscr...@googlegroups.com.
To post to this group, send email to clojurescript@googlegroups.com.
Visit this group at http://groups.google.com/group/clojurescript.

Reply via email to