Javascript 检查 jQuery 中是否为 undefined 和 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14995904/
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
Checking if undefined and null in jQuery
提问by K Groll
I'm a bit confused when checking if null or undefined and if I should be using !==or !=and "undefined" or undefined.
在检查是否为空或未定义以及是否应该使用!==或!=和“未定义”或未定义时,我有点困惑。
Here is some code I'm working on. Where am I going wrong with my null/unudefined etc?
这是我正在处理的一些代码。我的空/未定义等哪里出了问题?
var c = (jQuery(this).prop("target") != null && jQuery(this).prop("target") != undefined && jQuery(this).prop("target").toLowerCase() == "_blank") ? 1 : 0;
Thanks
谢谢
回答by the system
In general, keep it simple.
一般来说,保持简单。
To check for undefined, use:
要检查undefined,请使用:
foo === undefined
foo !== undefined
To check for null, use:
要检查null,请使用:
foo === null
foo !== null
To check for either at the same time, use:
要同时检查两者之一,请使用:
foo == null
foo != null
And in any case, store your .prop()to a variable to keep it clean. But in your case, if it equals "_blank", then you know it isn't nullor undefined, so:
无论如何,将您的存储.prop()到变量以保持其清洁。但在你的情况下,如果它等于"_blank",那么你知道它不是null或undefined,所以:
var targ = jQuery(this).prop("target").toLowerCase();
var c = targ === "_blank" ? 1 : 0;
Or you could make it even shorter by coercing the booleanto a number:
或者您可以通过boolean将 a强制转换为更短number:
var targ = jQuery(this).prop("target").toLowerCase();
var c = +(targ === "_blank");
These last two solutions are safe because .prop()will always return a string.
最后两个解决方案是安全的,因为.prop()将始终返回string.
回答by Joseph
Both
nullandundefinedare "falsy" values, thus they can be checked like they were boolean values. Thus, there's no sense comparing tonullandundefinedexcept for certain situations where you need to know if they are such values.when comparing, it's best to use strict comparison (like
===,!==and so on)the
&&in a condition does not evaluate the following condition if the one preceeding it is "falsy".You don't even need jQuerysince
thisis your DOM object (presumably an<a>) and you are trying to get thetargetproperty:
这两个
null和undefined是“falsy”的价值观,因此,他们可以像他们布尔值检查。因此,在某些情况下,您需要知道它们是否是这样的值,null并且undefined除此之外没有任何意义。比较时,最好使用严格比较(例如
===,!==等等)的
&&,如果一个前述它是“falsy”在条件不评估以下条件。您甚至不需要 jQuery,因为
this是您的 DOM 对象(大概是<a>)并且您正在尝试获取该target属性:
In the end:
到底:
var c = (this.target && this.target.toLowerCase() === "_blank") ? 1 : 0;
回答by Code.Town
This is the best way to checking undefined:
这是检查未定义的最佳方法:
if(typeof variable_here != 'undefined'){
// your code here.
};
And this is the best way to checking null:
这是检查空值的最佳方法:
if(variable_here !== null){
// your code here.
};
So your code should be like this:
所以你的代码应该是这样的:
var c = (jQuery(this).prop("target") !== null && typeof jQuery(this).prop("target") !== 'undefined' && jQuery(this).prop("target").toLowerCase() == "_blank") ? 1 : 0;
回答by Rohidas Kadam
I got something like this which is not relevent to your question but will help you
我得到了这样的东西,这与你的问题无关,但会帮助你
var targ = jQuery(this).prop("target").toLowerCase();
now if you want to check whether targ is null or undefined
现在如果你想检查 targ 是 null 还是 undefined
var c = (!targ || "") ? 1 : 0
hope this will help you
希望能帮到你
回答by Prince Prasad
In JavaScript, you are not required to explicitly check if a variable is null or undefined because:
在 JavaScript 中,您不需要显式检查变量是否为 null 或未定义,因为:
null or undefined return false in a boolean expression.
JS expressions are evaluated from left to right. So for a || b, if a is false, then only b will be evaluated. But if a is true, b will not be checked. Similarly for a && b if a is false, b will not be evaluated.
null 或 undefined 在布尔表达式中返回 false。
JS 表达式从左到右计算。所以对于一个 || b, 如果 a 为假,则只计算 b。但如果 a 为真,则不会检查 b。同样,对于 a && b,如果 a 为假,则不会评估 b。
Hence, if (a != null) { "do something" } can be written as if (a) { "do something" } or simply a && "do something".
因此, if (a != null) { "do something" } 可以写成 if (a) { "do something" } 或者简单地 a && "do something"。
In the same way, it can be used to set a default value when the value is set to null or undefined:
同理,当值设置为 null 或 undefined 时,可用于设置默认值:
function someFunction(age){
var age= age|| 18;
}
参考:https: //codereview.stackexchange.com/questions/472/usage-of-the-ternary-operator-with-functions-listening-to-click-events
回答by Lg102
Since undefined would be the variable type, use typeof
由于 undefined 将是变量类型,因此使用 typeof
var c = (
$(this).attr("target") != NULL &&
typeof $(this).attr("target") != "undefined" &&
$(this).attr("target").toLowerCase() == "_blank"
) ? 1 : 0;
I think, however, that you only need the last check. Is target "_blank", cneeds to be 1, otherwise, 0. Does it really matter if targetis even set?
但是,我认为您只需要最后一次检查。是 target "_blank",c必须是1,否则,0。target甚至设置是否真的很重要?
Also, use the attr()methode to get attributes, since prop()if for properties like selectedIndexor tagName.
此外,使用attr()方法来获取属性,因为prop()if 对于像selectedIndexor 之类的属性tagName。
回答by Aesthete
Don't get the same property 3 times just to check a value.
不要仅仅为了检查一个值而获得相同的属性 3 次。
var c = 0;
var prop = $(this).prop("target");
if(prop && prop.toLowerCase() === "_blank") c = 1;

