JS interop - property access
Update - July 4, 2017
The original post is now outdated. Instead use goog.object/get
, goog.object/getValueByKeys
, and goog.object/set
.
Example from the cljs api:
See CLJS-2148 and CLJS-2149 for more details.
The rest of this post is now outdated
Konrad Garus has a good post explaining the details of accessing js properties from cljs. It’s worth your time to read the entire post, but here are some general guidelines.
Use .-property
for cljs
In the normal case, you’ll want to use .-property
to access js properties. In the line-reader example, we use .-_lastLineData
to get/set data on this
.
Using .-property
allows the compiler to rename the property during advanced compilation, e.g. _lastLineData
could be renamed a
. (.-property object)
is the idiomatic way to access js properties.
Use (aget object "property")
for js
If you want your code to be accessible in js, use aget
/aset
with strings referring to the property.
Don’t mix accessors
If you sometimes use (.-property object)
and other times use (aget object "property")
, you’ll run into confusing bugs when it comes time to ship. There are few things as frustrating as bugs that only show up in prod (where you’re using advanced compilation). Be consistent, and you’ll save yourself unnecessary headaches.