Clojuratcript للتحقق مما إذا كان هناك عقار موجود في كائن JS

StackOverflow https://stackoverflow.com//questions/22046889

  •  21-12-2019
  •  | 
  •  

سؤال

كيف أكتب ما يلي في Clojurescript؟

giveacodicetagpre.

هل كانت مفيدة؟

المحلول

متابعة الطريقة المقبولة حتى الآن للتحقق مما إذا كانت خاصية كائن JS موجودة باستخدام الطريقة " hasownproperty " يمكننا ترجمة ذلك على النحو التالي:

giveacodicetagpre.

نصائح أخرى

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