プロパティがJSオブジェクトに存在するかどうかを確認するためのCloJurescript
-
21-12-2019 - |
質問
Clojurescriptに次のように書いていますか?
obj = {"a" : 4};
"a" in obj;
. 解決
メソッド " 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