CLJS: Hidden Google Closure Library gems

To get the ball rolling, here’s a first example. goog.object/get allows easy js-obj property access by key. It works just like (clojure.core/get m k)

cljs.user=> (goog.object/get #js{"a" 123} "a")
123

(goog.object/get m k) won’t throw, even if k is not in m or if k or m are nil. You can also supply an optional third argument to specify a default value, if the key is missing.

Next, and similar to clojure.core/get-in, goog.object/getValueByKeys allows nested property access. Note, however, that unlike get-in, it expects each key as a separate argument:

cljs.user=> (goog.object/getValueByKeys #js{"a" #js{"b" 123}} "a" "b")
123
4 Likes