如何在CLOJURESCRIPT中写下以下内容?

obj = {"a" : 4};
"a" in obj;
.

有帮助吗?

解决方案

在接受的等方式检查JS对象属性是否存在使用该方法“ hasownproperty ”我们可以如下翻译:

(def foo (js-obj "bar" "baz"))
(.hasOwnProperty foo "bar")
;; => true
(.-bar foo)
;;=> "baz"
(.hasOwnProperty foo "car")
=> false
(.-car foo)
;;=> nil
.

其他提示

exists? was added to check for undefined in ClojureScript :

   (ns my.ns
     (:require-macros [cljs.core :refer [exists?]]))

   (if (exists? js/jQuery)
      (println "jQuery"))
      (println "no jQuery"))

One can also use aget and nil? to avoid calling JavaScript functions :

(def scope (js-obj))
(aset scope "var1" "Value")
(aget scope "var1")               ;; "Value"
(aget scope "anotherVar")         ;; nil
(nil? (aget scope "anotherVar"))  ;; true
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top