문제

ClojureScript에 다음을 어떻게 씁니까?

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

도움이 되었습니까?

해결책

js object 속성이 " 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