检查jQuery中是否为undefined和null [英] Checking if undefined and null in jQuery

查看:88
本文介绍了检查jQuery中是否为undefined和null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在检查是否为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;

谢谢

推荐答案

  • nullundefined都是假"值,因此可以像检查布尔值一样对其进行检查.因此,将nullundefined进行比较是没有意义的,除了某些情况下,您需要知道它们是否是这样的值.

    • 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆