测试ClojureScript中是否存在某些内容的方法是什么?例如,我正在尝试访问浏览器地理位置API。在javascript中,我会做一个简单的检查:
screen width
但是将它翻译成ClojureScript,我收到一个错误:
screen height
在ClojureScript中检查浏览器功能的正确方法是什么?
答案 0 :(得分:5)
有多种选择:
boolean foo = getPreferences(Context.MODE_PRIVATE).getBoolean(getString(R.string.mm_preferences_numbers_as_fractions_key), true);
boolean bar = getPreferences(Context.MODE_PRIVATE).getBoolean(getString(R.string.mm_preferences_comma_as_decimal_separator_key), true);
:
http://dev.clojure.org/jira/browse/CLJS-495 只存在于clojurescript而不是clojure。
如果您查看宏in core.cljc,您会发现它只是exists?
。
使用示例:
if( typeof ... !== 'undefined' )
(if (exists? js/navigator.geolocation)
(println "Geolocation is supported"))
(println "Geolocation is not supported"))
扩展为(js-in "geolocation" js/window)
。
"geolocation" in windows
扩展为(undefined? js/window.geolocation)
IMO,正确的是void 0 === window.geolocation
。
答案 1 :(得分:1)
目前,我正在使用:
(if (not (nil? js/navigator.geolocation))
(println "Geolocation is supported")
(println "Geolocation is not supported"))
不确定这个惯用/覆盖所有用例,我很乐意接受另一个答案,并给出正确的解释。