Preferred way to access a JavaScript static property?

528 views
Skip to first unread message

Xavi Caballé

unread,
Oct 20, 2013, 1:04:31 PM10/20/13
to clojur...@googlegroups.com
Since ClojureScript 1933, the compiler emits a warning when accessing a static property of a JavaScript class using a "/". For example, when referring to a property of XPathResult [1]:


XPathResult/ANY_TYPE
; => WARNING: No such namespace: XPathResult

The following 3 forms seem to be equivalent and work without warnings...

XPathResult.ANY_TYPE
js/XPathResult.ANY_TYPE
(.-ANY_TYPE js/XPathResult)

Is any of these preferable over the others?

On the other hand, in Clojure a "/" can still be used (without warnings) to access a Java class static field [2]. For example [3]

Math/PI

So I wonder... why the ClojureScript syntax to access the static property of a class is different than in Clojure?

Thanks,
Xavi


[1] https://developer.mozilla.org/en/docs/XPathResult
[2] http://clojure.org/java_interop
[3] There's also a Math class in JavaScript ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/PI ), but I've not used it in the ClojureScript example above because Math/PI doesn't generate any warning, apparently because Math is an exception from what I understand from this commit
https://github.com/clojure/clojurescript/commit/4769c5e05233d9841aedf882bd99812d0f842abb

Jonas Enlund

unread,
Oct 20, 2013, 2:57:47 PM10/20/13
to clojur...@googlegroups.com
Hi

The two forms that (I think) are future proof are (.-ANY_TYPE js/XPathResult) and js/XPathResult.ANY_TYPE. The latter is supported since it's the only way to call constructors like (new js/Foo.Bar 1 2 3) ((new (.-Bar js/Foo) 1 2 3) does not work, but maybe it should?).

My opinion on whether something like XPathResult/ANY_TYPE should be supported or not is laid out at https://groups.google.com/d/msg/clojure-dev/Ge5uEqlUcJk/E-PnFQ9lefoJ

David Nolen

unread,
Oct 21, 2013, 11:37:34 AM10/21/13
to clojur...@googlegroups.com
There's no such that as static properties in JavaScript, this is a JVM ism, and JavaScript does not provide adequate reflective capabilities to support detecting "classes". / can only be used to delimit namespaces in ClojureScript.

js/XPathResult.ANY_TYPE

or the property access variants suggested by Jonas are the only correct forms.

David



--
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 clojurescrip...@googlegroups.com.
To post to this group, send email to clojur...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojurescript.

Reply all
Reply to author
Forward
0 new messages