zoukankan      html  css  js  c++  java
  • 怎样理解undefined和 null

    前言: undefined表示 "未定义", null 表示 "空"

    第一步: 一般在变量或属性没有声明或者声明以后没有赋值时, 这个变量的值就是undefined;

    typeof a; // "undefined";
    
    var b;
    typeof(b); // "undefined";
    
    var c = {};
    typeof(c.name); // "undefined";

    第二步: null是检测变量指向的内存地址是否存在, 即: 如果变量不指向任何一个内存地址, 则返回null.

    document.getElementById("fasdf")
    // null
    Object.prototype.__proto__;
    // null

      

    注意:

    null 和 undefined 都是基本类型, 使用instanceof检测类型时都会返回false; 但null非常特殊, 使用typeof去判断时会返回object, 但是有instanceof判断时又会返回false; 一般判断某个变量是否为null, 使用的是: null === null;

    null instanceof Object; // false
    undefined instanceof Object; // false
    
    typeof null; // "object";
    typeof undefined; // "undefined";
  • 相关阅读:
    时间加减天数
    时间加减秒数
    什么BOM?
    js 事件基础
    js 九九乘法
    CSS3 动画基础单词语法
    css3 3D转换 基础语法
    css3 2D 转换 基础语法
    js onchange案例
    js之冒泡排序
  • 原文地址:https://www.cnblogs.com/aisowe/p/11634836.html
Copyright © 2011-2022 走看看