zoukankan      html  css  js  c++  java
  • js判断是否为空

     

    (1)typeof用法

    typeof的运算数未定义,返回的就是 "undefined".

    运算数为数字 typeof(x) = "number"

    字符串 typeof(x) = "string"

    布尔值 typeof(x) = "boolean"

    对象,数组 和null typeof(x) = "object"

    函数 typeof(x) = "function"

    (2)js判断是否为空

    var exp = null;
    if (!exp && typeof(exp)!="undefined" && exp!=0)
    {
        alert("is null");
    } 

    尽管如此,我们在 DOM 应用中,一般只需要用 (!exp) 来判断就可以了,因为 DOM 应用中,可能返回 null,可能返回 undefined,如果具体判断 null 还是 undefined 会使程序过于复杂。

    以下是不正确的用法:

    var exp = null;
    if (exp == null)
    {
        alert("is null");
    }

    exp 为 undefined 时,也会得到与 null 相同的结果,虽然 null 和 undefined 不一样。

    var exp = null;
    if (!exp)
    {
        alert("is null");
    }

    如果 exp 为 undefined 或者数字零,也会得到与 null 相同的结果,虽然 null 和二者不一样。

    var exp = null;
    if (typeof(exp) == "null")
    {
        alert("is null");
    }

    为了向下兼容,exp 为 null 时,typeof 总返回 object。

    var exp = null;
    if (isNull(exp))
    {
        alert("is null");
    }

    JavaScript 中没有 isNull 这个函数。

  • 相关阅读:
    Create Your Tab and LayerTabMenu In Katana
    Linux C Programing
    Linux C Programing
    dynamics_cast<>
    TBB 学习笔记
    冒泡排序算法
    Spring4学习笔记:Spring框架中为一个bean配置依赖注入的方式
    基数排序算法的Java实现
    堆排序算法的Java实现与分析
    贪心算法
  • 原文地址:https://www.cnblogs.com/it-xiaoJiang/p/5736673.html
Copyright © 2011-2022 走看看