如何在jQuery中检查'undefined'值 [英] How to check 'undefined' value in jQuery
问题描述
我们如何添加对未定义变量的检查,喜欢:
How we can add a check for an undefined variable, like:
function A(val) {
if (val == undefined)
// do this
else
// do this
}
推荐答案
JQuery库是专门为简化和统一某些JavaScript功能而开发的。
JQuery library was developed specifically to simplify and to unify certain JavaScript functionality.
但是如果你需要针对<$ c $检查一个变量c> undefined 值,不需要发明任何特殊方法,因为JavaScript有 typeof
运算符,简单,快速且跨平台:
However if you need to check a variable against undefined
value, there is no need to invent any special method, since JavaScript has a typeof
operator, which is simple, fast and cross-platform:
if (typeof value === "undefined") {
// ...
}
它返回一个字符串,指示变量或其他未评估操作数的类型。与 if(value === undefined){...}
相比,此方法的主要优点是 typeof $ c $如果变量
value
不存在,c>永远不会引发异常。
It returns a string indicating the type of the variable or other unevaluated operand. The main advantage of this method, compared to if (value === undefined) { ... }
, is that typeof
will never raise an exception in case if variable value
does not exist.
这篇关于如何在jQuery中检查'undefined'值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!