How can I get the Clojurescript namespace I am in from within a clojurescript program?

后端 未结 4 756
长发绾君心
长发绾君心 2021-01-12 08:16

How can I get the Clojurescript namespace I am in from within a clojurescript program? I want to do this do provide certain debug information (it only needs to work in dev m

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-12 08:30

    You can get the name of the current namespace with this trick, which takes advantage of :: creating a namespaced symbol for you in the current namespace:

    (namespace ::x)
    

    You probably don't want to use that value for anything, because if the code is compiled the internal representation will change. If you want to live dangerously, then in the browser you can then access the js object that holds the namespace like this:

    (reduce (fn [ns n] (aget ns n))
            js/window
            (clojure.string/split (namespace ::x) #"\."))
    

提交回复
热议问题