javascript 如何检查变量或对象是否未定义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8531059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a variable or object is undefined?
提问by Yash Desai
I always thought I could just check an undefined var by comparing it to undefined, but this is the error I get in the chrome console :
我一直认为我可以通过将未定义的 var 与未定义进行比较来检查它,但这是我在 chrome 控制台中得到的错误:
How how do i check for the object jQuery being undefined ?
我如何检查对象 jQuery 是否未定义?
EDIT :
编辑 :
if(jQuery) is giving me problems too
if(jQuery) 也给我带来了问题
EDIT :
编辑 :
solution :
解决方案 :
if(window.jQuery)
works.
typeof(jQuery) == 'undefined'
works too.
if(window.jQuery)
作品。
typeof(jQuery) == 'undefined'
也有效。
Could anyone explain why ?
谁能解释为什么?
回答by
There are several solutions:
有几种解决方案:
Use
typeof
. It is a special operator and will never result in aReferenceError
. It evaluates to "undefined" for, well, theundefined
value orfor a variable which does not exist in context. I'm not a fan of it, but it seems very common.Use
window.jQuery
. This forces a "property lookup": property lookups never fail, and returnundefined
if said property does not exist. I've seen it used in some frameworks. Has the downside of assuming a context (usuallywindow
).Make sure the variable is "declared":
var jQuery; if (jQuery) { /* yay */ }
. Doesn't seem very common, but it is entirely valid. Note thatvar
is just an annotation and is hoisted. In the global context this will create the "jQuery" property.Catch the
ReferenceError
. Honestly, I have never seen this nor do I recommend it, but it would work.
使用
typeof
. 它是一个特殊的运算符,永远不会产生ReferenceError
. 对于上下文中不存在的undefined
值或变量,它的计算结果为“未定义” 。我不喜欢它,但它似乎很常见。使用
window.jQuery
. 这会强制进行“属性查找”:属性查找永远不会失败,undefined
如果所述属性不存在则返回。我已经看到它在一些框架中使用过。具有假设上下文的缺点(通常为window
)。确保变量“声明”:
var jQuery; if (jQuery) { /* yay */ }
。看起来并不常见,但它完全有效。请注意,这var
只是一个注释并被提升。在全局上下文中,这将创建“jQuery”属性。抓住
ReferenceError
. 老实说,我从未见过这个,也不推荐它,但它会起作用。
Happy coding.
快乐编码。
回答by Ariful Islam
Process 1:
流程一:
if (jQuery) {
// jQuery is loaded
} else {
// jQuery is not loaded
}
Process 2:
过程2:
if (typeof jQuery == 'undefined') {
// jQuery is not loaded
} else {
// jQuery is loaded
}
回答by Preet Sangha
回答by handy
As far as I know you can do
据我所知你可以做到
if(typeof X == 'undefined')
But there are resources loader you might want to take a look at. And the answer given before me is also correct.
但是您可能需要查看一些资源加载器。而我之前给出的答案也是正确的。
回答by Lerous
The variable called "jQuery" in your code has never been declared, so it will throw an error like "xxx(variable name) is not defined".
您的代码中名为“jQuery”的变量从未被声明过,因此它会抛出类似“xxx(variable name) is not defined”之类的错误。
You can use the typeof
operator to check is a variable is undefined or not
您可以使用typeof
运算符来检查变量是否未定义
if (typeof(jQuery) == "undefined")
回答by Ahmed El Kilani
You can use : if( typeof jQuery !== 'undefined')
您可以使用: if( typeof jQuery !== 'undefined')
or
或者
Do what is recommended by mozilla
做 mozilla 推荐的事情
if('jQuery' in window)
if('jQuery' 在窗口中)
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/undefined
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/undefined
回答by maialithar
if(jQuery)
should be enough, shouldn't it?
if(jQuery)
应该够了吧?