d3.js - How to call a function (requiring args) with string name - Clojurescript | d3


Keywords:d3.js 


Question: 

I'm trying to do some refactoring on a function (set-attr) that calls a function (.attr) on a d3 object (node). I want to be able to decouple the object function (.attr) from set-attr.

But I keep getting this error Cannot read property 'each' of null.

I'm hoping someone has experience with d3 interop in Cljs and knows of something that can work, because it works fine on regular js objects.

Function that works

(defn set-attr [node [attr settr]]
  (.attr node attr settr))

What I think should work (but gives me the error)

(defn set-attr [node [attr settr]]
  ((aget node "attr") attr settr))

Works fine on a regular js object

((aget (clj->js {:foo (fn [x y] (str "hi" x y))}) "foo" ) "ryan" "lol")
;; => "hiryanlol"

1 Answer: 

d3 uses inheritance. Thus you cannot process this object as a mere hash-table. You have to reach to its prototype to get what you want, which is usually not a good idea: this would produce rather brittle code.