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";
  • 相关阅读:
    Gym
    [APIO2014] 回文串
    python选课系统
    python面向对象之类成员修饰符
    python面向对象之类成员
    python的shelve模块
    python的re模块
    python的configparser模块
    python的sys和os模块
    python的hashlib模块
  • 原文地址:https://www.cnblogs.com/aisowe/p/11634836.html
Copyright © 2011-2022 走看看