Home > clojure > I did a bad thing with ClojureScript

I did a bad thing with ClojureScript

UPDATE: Don’t do this. It’s a joke and (js*) is an internal detail of ClojureScript. If you need to access native code, access it through the js namespace.


Tonight ClojureScript was announced and made available. I’m interested in how JavaScript interop works for environments like node.js, CouchDB, etc. So far, it seems like (js*) is the main entry point to JavaScript land and functions like (aset) can also be used:

$ script/repljs 
#'user/jse
"Type: " :cljs/quit " to quit"
ClojureScript:cljs.user> (def something (js* "{x: 100}"))
#<[object Object]>
ClojureScript:cljs.user> (.x something)
100
ClojureScript:cljs.user> (aget something "x")
100
ClojureScript:cljs.user> (aset something "y" 99)
99
ClojureScript:cljs.user> (.y something)
99

It happens that repljs (the ClojureScript repl) runs in Rhino which is ripe for abuse:

$ script/repljs 
#'user/jse
"Type: " :cljs/quit " to quit"
ClojureScript:cljs.user> (def f 
  (js* "new Packages.javax.swing.JFrame(\"ClojureScript!!\")"))
#<javax.swing.JFrame ... >
ClojureScript:cljs.user> (js* "~{f}.setVisible(true)")
#<undefined>

Yes. I am a bad person. I will try to do something useful with ClojureScript shortly.

Also note the interpolation syntax, ~{f}, for referring to Clojure bindings in JavaScript.

Categories: clojure Tags: ,
  1. devn
    July 23rd, 2011 at 14:53 | #1

    (js*) Considered Harmful

  2. July 23rd, 2011 at 15:22 | #2

    @devn
    Yep. Hopefully no one takes this too seriously. Maybe I should add a disclaimer.

    Has an “official” JavaScript interop approach been documented yet? For example, if I want to “import” CouchDB’s emit function into my ClojureScript code, is (def emit (js* “emit”)) the right approach or will there be a more official import mechanism at some point?

    Programmers being programmers there are undoubtedly dozens of people out there wrapping every bit of JavaScript they can get their hands on using (js*). It would be a shame if they were doing it wrong :)

  1. No trackbacks yet.