Help request: clojurescript macro for object property access

41 views
Skip to first unread message

Dave Sann

unread,
Dec 19, 2011, 8:58:27 PM12/19/11
to clo...@googlegroups.com
I would like to have a macro to do the following, is this possible?

(getm javascript-map "key")

expands to:

(.key javascript-map)

This is to manage situations where a javascript map/object is accessed by string key.
In advanced optimisations mode this will fail.
The key must be accessed as a property access for the compiler to manage correctly.

Thanks in advance

Dave


David Nolen

unread,
Dec 19, 2011, 9:20:12 PM12/19/11
to clo...@googlegroups.com
On Mon, Dec 19, 2011 at 8:58 PM, Dave Sann <dave...@gmail.com> wrote:
I would like to have a macro to do the following, is this possible?

(getm javascript-map "key")

expands to:

(.key javascript-map)

What advantage does the former have over the latter? The latter is shorter. Also the new property syntax is just around the corner:

(.-key js-map)

David 

Dave Sann

unread,
Dec 19, 2011, 9:44:36 PM12/19/11
to clo...@googlegroups.com
I will give a concrete example as explanation.

In pinot.events there is a function "on"

(on elems :click event_handler)

:click goes on to be looked up in the javascript map EventType in goog.events  

This lookup is done by string - which is fine in unoptimised code but does not work in advanced mode.

I can modify the function and then write (on elem (.CLICK gevents/EventType) f)
but this is not as nice.

This is why I would like to have the macro.

Is there an alternative way to achieve this?

cheers

Dave


Dave Sann

unread,
Dec 19, 2011, 10:36:53 PM12/19/11
to clo...@googlegroups.com
Nevermind, I think I can see why this is a non-starter....

Dave Sann

unread,
Dec 19, 2011, 11:31:04 PM12/19/11
to clo...@googlegroups.com
The corollary of this would be that, in advanced mode, there is no way to pass a property key to a function to access a js object property because the key would be munged.

Right?

In which case - you need to be careful when attempting this sort of access.

For the case above, for now, I ported the file to clojurescript - which actually took all of 5 mins. maintenance may be an issue.

Cheers

Dave


Dave Sann

unread,
Dec 19, 2011, 11:32:14 PM12/19/11
to clo...@googlegroups.com
To clarify, the key in the map/object would be munged.
Reply all
Reply to author
Forward
0 new messages