null 与 undefind 的区别 及数据类型 以及检测数据类型

** 数据类型**
答:基本数据类型: null undefind number string boolean bigint Symbol(ES6 引入的一种新的原始数据类型Symbol,表示独一无二的值。)
引用数据类型: object
Symbol: 由于每一个 Symbol 值都是不相等的,这意味着只要 Symbol 值作为标识符,用于对象的属性名,就能保证不会出现同名的属性

JS基本类型和引用类型的存储方式
https://www.cnblogs.com/qFire/p/16774336.html
https://blog.csdn.net/never_33/article/details/129212638

** null 与 undefind 的区别 **
答 undefind 代表缺少值, 声明变量但是没有进行赋值,null 代表空对象指针,表示 "什么都没有"。检测数据类型为object,null 和 undefined 的值相等,但类型不等

** 及为啥null的类型为objec**
答:基本数据类型存储在32位的二进制中,低三位的数字用于表示该数据的类型,
000 代表object , null 全为0, 所以误认为object

检测数据类型
答:1: **typeof **
typeof 'a'; // string 有效
typeof 1; // number 有效
typeof true; //boolean 有效
typeof Symbol(); // symbol 有效
typeof undefined; //undefined 有效
typeof new Function(); // function 有效
typeof null; //object 无效
typeof [1] ; //object 无效
typeof new RegExp(); //object 无效
typeof new Date(); //object 无效

  1. A instanceof B(A是不是B的实例,返回布尔值)
    [] instanceof Array; // true
    new Date() instanceof Date;// true
    function Person(){};
    new Person() instanceof Person;//true
    [] instanceof Object; // true
    new Date() instanceof Object;// true
    new Person instanceof Object;// true
    注意:instanceof 只能用来判断两个对象是否属于实例关系, 而不能判断一个对象实例具体属于哪种类型
    3.constructor(F利用原型对象上的 constructor 引用了自身,当 F 作为构造函数来创建对象时,原型上的 constructor 就被遗传到了新创建的实例对象上)
注意:1. null 和 undefined 是无效的对象,因此是不会有 constructor 存在的,这两种类型的数据需要通过其他方式来判断。
     2. 函数的 constructor 是不稳定的,这个主要体现在自定义对象上,当开发者重写 prototype 后,原有的 constructor 引用会丢失,constructor 会默认为 Objec
  1. Object.prototype.toString.call(value)
    (toString() 是 Object 的原型方法,调用该方法,默认返回当前对象的 [[Class]] 。这是一个内部属性,其格式为 [object Xxx] ,其中 Xxx 就是对象的类型)
    Object.prototype.toString.call('') ; // [object String]
    Object.prototype.toString.call(1) ; // [object Number]
    Object.prototype.toString.call(true) ; // [object Boolean]
    Object.prototype.toString.call(Symbol()); //[object Symbol]
    Object.prototype.toString.call(undefined) ; // [object Undefined]
    Object.prototype.toString.call(null) ; // [object Null]
    Object.prototype.toString.call(new Function()) ; // [object Function]
    Object.prototype.toString.call(new Date()) ; // [object Date]
    Object.prototype.toString.call([]) ; // [object Array]
    Object.prototype.toString.call(new RegExp()) ; // [object RegExp]
    Object.prototype.toString.call(new Error()) ; // [object Error]
    Object.prototype.toString.call(document) ; // [object HTMLDocument]
    Object.prototype.toString.call(window) ; //[object global] window 是全局对象 global 的引用

参考文献:https://wenku.baidu.com/view/72ee6637cf7931b765ce0508763231126edb77ec.html
https://blog.csdn.net/qq_44552416/article/details/123186262
https://m.php.cn/article/477684.html

posted @ 2022-05-30 22:35  小白张先生  阅读(219)  评论(0)    收藏  举报