在检查是否为null或undefined以及是否应该使用!==或!=以及"undefined"或undefined时,我有些困惑.
I'm a bit confused when checking if null or undefined and if I should be using !== or != and "undefined" or undefined.
这是我正在处理的一些代码.我的null/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;谢谢
推荐答案
-
null和undefined都是假"值,因此可以像检查布尔值一样对其进行检查.因此,将null和undefined进行比较是没有意义的,除了某些情况下,您需要知道它们是否是这样的值.
Both null and undefined are "falsy" values, thus they can be checked like they were boolean values. Thus, there's no sense comparing to null and undefined except 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".
您甚至不需要jQuery ,因为this是您的DOM对象(大概是<a>),而您正在尝试获取target属性:
You don't even need jQuery since this is your DOM object (presumably an <a>) and you are trying to get the target property:
最后:
var c = (this.target && this.target.toLowerCase() === "_blank") ? 1 : 0;
更多推荐
检查jQuery中是否为undefined和null
发布评论