ClojureScript가 JS 객체에 속성이 있는지 확인하기 위해
-
21-12-2019 - |
문제
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
제휴하지 않습니다 StackOverflow