what is "js/" in clojurescript?

1,535 views
Skip to first unread message

Rob

unread,
May 3, 2012, 12:12:51 AM5/3/12
to clo...@googlegroups.com
Hi,

Syntax like this doesn't work in normal Clojure, right?

    js/document.body.style

It just did in a ClojureScript repl.  Is there something magic about "js/" ?  What is it?

thanks,
Rob

Tamreen Khan

unread,
May 3, 2012, 12:17:03 AM5/3/12
to clo...@googlegroups.com
A quick explanation is that functions/other javascript objects that otherwise exist in the global namespace (ie. document, console, window) are accessed through the js/ in Clojurescript. This is a JS-specific thing and therefore you don't find js/ in regular Clojure.

However, doing the form (MyClass/MyStaticMethod arg1 arg2 ...) *does* exist in Clojure. It's a way of calling static Java methods or accessing static fields. See  http://clojure.org/java_interop for more info.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Rob

unread,
May 3, 2012, 12:50:25 AM5/3/12
to clo...@googlegroups.com


On Thursday, May 3, 2012 12:17:03 AM UTC-4, Tamreen Khan (Scriptor) wrote:

However, doing the form (MyClass/MyStaticMethod arg1 arg2 ...) *does* exist in Clojure. It's a way of calling static Java methods or accessing static fields. See  http://clojure.org/java_interop for more info.

Okay.  It seems like normal Clojure the thing after the slash can't have more '.' characters in it, so I'm wondering if that's the extend of the difference.  Maybe that difference is not on purpose.


David Nolen

unread,
May 3, 2012, 11:47:12 AM5/3/12
to clo...@googlegroups.com
I would not rely on this behavior. Follow Clojure's property access conventions.

David 
--

Devin Walters

unread,
May 3, 2012, 4:17:00 PM5/3/12
to clo...@googlegroups.com
Some additional information if you're interested:
http://dev.clojure.org/jira/browse/CLJS-89
http://dev.clojure.org/display/design/Unified+ClojureScript+and+Clojure+field+access+syntax

'(Devin Walters)
> To post to this group, send email to clo...@googlegroups.com (mailto:clo...@googlegroups.com)
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com (mailto:clojure+u...@googlegroups.com)

Peter Taoussanis

unread,
Jan 7, 2013, 8:09:44 AM1/7/13
to clo...@googlegroups.com
> I would not rely on this behavior. Follow Clojure's property access conventions.

Sorry to dig this up again - would just like to clarify:

The idiomatic way of accessing something like `window.location.pathname` then would be (aget js/window "location" "pathname"), or nested .- accesses, correct? I know things with Cljs are developing quickly and I keep seeing the `js/window.location.pathname` form pop up (including in Clojurescript's samples/twitterbuzz/src/twitterbuzz/anneal.cljs), so just wanted to double check.

Assuming the latter form is indeed incorrect, any chance of adding a compiler warning to start dissuading people from using it?

Thanks!

David Nolen

unread,
Jan 7, 2013, 8:39:28 AM1/7/13
to clo...@googlegroups.com
Yes a warning would be great as well as fixing the examples.

Ticket & patch welcome.
--

Peter Taoussanis

unread,
Jan 7, 2013, 8:58:25 AM1/7/13
to clo...@googlegroups.com
Thanks David.

Ticket & patch welcome.

I've been lazy / holding out for the electronic CA, but I'll make a note to come back to this if no one else steps up.

Cheers!

Jonas

unread,
Jan 12, 2013, 7:34:56 AM1/12/13
to clo...@googlegroups.com
Hi

I created an issue+patch on JIRA: http://dev.clojure.org/jira/browse/CLJS-455

Jonas

David Nolen

unread,
Jan 12, 2013, 11:02:43 AM1/12/13
to clo...@googlegroups.com
Thanks!
--

Roberto Agostino Vitillo

unread,
Aug 17, 2014, 9:04:47 AM8/17/14
to clo...@googlegroups.com
As noted in the BR, it seems like JS style access after the / was actually intended. Is this behaviour documented somewhere? I see more and more examples in the wild that exploit this feature, it would be great to have it documented somewhere as it can cause confusion.
Reply all
Reply to author
Forward
0 new messages