【问题标题】:How do I get properties with dashes in their names in clojurescript?如何在 clojurescript 中获取名称中带有破折号的属性?
【发布时间】:2013-02-24 06:51:17
【问题描述】:

我想要获取其值的 javascript 对象中有一个名为“user-agent”的属性。我如何在 clojurescript 中做到这一点?

(js/eval "a = {'user-agent': 'curl/7.22.0'}")
(js/eval "a['user-agent']") ;=> curl/7.22.0
(.-user-agent js/a) ;=> (returns nothing)
(. js/a -user-agent) ;=> (returns nothing)

这是因为这里使用点表示法而不是括号表示法检索属性吗? https://github.com/clojure/clojurescript/blob/master/src/clj/cljs/compiler.clj#L734

【问题讨论】:

    标签: clojure clojurescript


    【解决方案1】:

    使用aget:

    (aget js/a "user-agent")
    

    点表示法不起作用,因为 clojurescript 编译器会进行一些名称修改以支持扩展,以便支持变量名称中的 ?! 等字符。除此之外,名称 munging 还会将破折号更改为下划线,以便将诸如 (.-user-agent js/a) 之类的字段访问编译为 a.user_agent 之类的内容。

    只要你留在 clojurescript 中,munging 这个名字是透明的,你通常不需要知道它,除非你在做 javascript 互操作。在这种情况下,您可以使用agetaset 等interpo 功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      • 2017-12-05
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多