zoukankan      html  css  js  c++  java
  • null与undefined

    null是一个表示"无"的对象,转为数值时为0;undefined是一个表示"无"的原始值,转为数值时为NaN。

    undefined和null在if语句中,都会被自动转为false,相等运算符甚至直接报告两者相等。

    Boolean(null);    //转为 false
    Boolean(undefined);    //转为 false
    
    null == undefined    //true
    
    null != undefined    //false
    
    null !== undefined    //true

    可以通过“===”来区分它们。

    null === undefined   //false

    它们的不同之处在于:

    1、typeof返回值不同

    typeof null;    //"object"
    typeof undefined;    //"undefined"

    2、转为数字结果不同

    Number(null);    //转为 0
    Number(undefined);    //转为 NaN

    它们的用法:

    1、null表示"没有对象",即该处不应该有值。典型用法是:

    (1) 作为函数的参数,表示该函数的参数不是对象。

    (2) 作为对象原型链的终点。

    Object.getPropertyOf(Object.prototype);    //null

    2、undefined表示"缺少值",就是此处应该有一个值,但是还没有定义。典型用法是:

    (1)变量被声明了,但没有赋值时,就等于undefined。

    (2) 调用函数时,应该提供的参数没有提供,该参数等于undefined。

    (3)对象没有赋值的属性,该属性的值为undefined。

    (4)函数没有返回值时,默认返回undefined。

    var i;
    console.log(i);    //undefined
    
    function f(x){
      console.log(x);  
    }
    f();    //undefined
    
    var o = new Object();
    o.p;    //undefined
    
    var x = f();
    x    //undefined
  • 相关阅读:
    数据库的隔离
    Maven的工程类型有哪些
    Redis中的常用命令哪些
    flume--exec源
    hadoop基本组件原理小总结
    Idea 激活
    hadoop中遇到的各种错误记录
    MySQL认知
    Python爬虫之post请求
    Python爬取ithome的一所有新闻标题评论数及其他一些信息并存入Excel中。
  • 原文地址:https://www.cnblogs.com/lengyuehuahun/p/8287807.html
Copyright © 2011-2022 走看看