一、JS的数据类型
1、基本数据类型
Number String Boolean Underfined Null
2、引用数据类型
Object
3、ES6新增的类型
Symbol
4、谷歌加的
bigInt
二、测试数据类型
1.先简单粗暴的看一下上面写的这几个数据类型是不是真的
console.log("typeof (Number): " + typeof (Number)); console.log("typeof (String): " + typeof (String)); console.log("typeof (Boolean): " + typeof (Boolean)); console.log("typeof (undefined): " + typeof (undefined)); console.log("typeof (null): " + typeof (null)); console.log("typeof (Object): " + typeof (Object)); console.log("typeof (Symbol): " + typeof (Symbol)); console.log("typeof (BigInt): " + typeof (BigInt));
讯享网
结果是这样的,不大对劲,就一个undefined打印出了我想要的结果
换个方式再来试一下,正规点的
讯享网 console.log("typeof (1): " + typeof (1)); console.log("typeof ('jack'): " + typeof ('jack')); console.log("typeof (true): " + typeof (true)); console.log("typeof (undefined): " + typeof (undefined)); console.log("typeof (null): " + typeof (null)); console.log("typeof ({}): " + typeof ({})); console.log("typeof (Symbol('id')): " + typeof (Symbol('id'))); console.log("typeof (BigInt('40995'): " + typeof (BigInt("40995")));
这次结果不错,基本上全是我想要的了,就一个null不大对头

2.null到底是什么
那就把null拿出来多试几次,看看有什么特别的地方
console.log("typeof (null): " + typeof (null)); console.log("typeof (Null): " + typeof (Null)); console.log("typeof (''): " + typeof ('')); console.log("typeof ('null'): " + typeof ('null')); //console.log("typeof ( ): " + typeof ( )); //console.log("typeof (): " + typeof ());
最后一行代码报错了,只能注释掉,倒数第二行放了个空格,也报错,不管怎么样,没一个数据是null类型的,传说中的null是不是不存在啊

百度搜索了下,大致有下面几个结果
1.js的数据是由标记位和数值来表示的,而object和null的标记位都是000,所以就被判断成了object,算是一个bug
2.null表示一个空指针对象,一个指针不知道往哪里指的对象,还是对象
3.null不是一个空引用, 而是一个原始值,它“期望”引用一个对象
不管哪种说法对,都不影响我们敲代码,所以这几种都当故事来看了
刚才搜索的时候又找到了一种判断数据类型的方法,先拿来再测试一下null
讯享网 console.log(Number(null)); console.log(typeof null); console.log(Object.prototype.toString.call(null));//刚找到的方法
竟然判断出null这个数据类型了,这就有意思了

又知道了一个新方法,那就把能想到的全都再敲一遍
console.log("111:"+ Object.prototype.toString.call(111)); console.log("jack':"+Object.prototype.toString.call('jack')); console.log("true:"+Object.prototype.toString.call(true)); console.log("undefined:"+Object.prototype.toString.call(undefined)); console.log("null:"+Object.prototype.toString.call(null)); console.log("{}:"+Object.prototype.toString.call({})); console.log("[]:"+Object.prototype.toString.call([])); console.log("Symbol('id'):"+Object.prototype.toString.call(Symbol('id'))); console.log("BigInt('40995'):"+Object.prototype.toString.call(BigInt('40995'))); console.log("new Date():"+Object.prototype.toString.call(new Date())); console.log("/[1-9][a-z]/:"+Object.prototype.toString.call(/[1-9][a-z]/)); console.log("function () {console.log('hello world')}:"+Object.prototype.toString.call(function () {console.log('hello world')}));
把能想到的,嗯,不对,把能抄到的全写了一遍,结果如下

由结果可知,js一共有12种数据类型,当然了,这是不可能的,哪来这么多!
最上面我查到的是八种:Number String Boolean Underfined Null Object Symbol BigInt
这里多出来的四种分别是:Array数组 Date日期 RegExp正则 Function函数,我觉得他们都是Object,最多也就是把正则归类到String,当然还是需要用代码说话
首先判断一个数组
讯享网console.log(typeof []); console.log(Object.prototype.toString.call([])); console.log([] instanceof Array); console.log([] instanceof Object); console.log(Array instanceof Object);
运行结果:[]是Array,而Array是Object,所以[]是Object数据类型

然后Date
let d = new Date(); console.log(typeof d ); console.log(Object.prototype.toString.call(d)); console.log(d instanceof Date); console.log(d instanceof Object); console.log(Date instanceof Object);
运行结果如下:说明时间也是Object类型

最后是正则
讯享网let d = /[0-9]/; console.log(typeof d ); console.log(Object.prototype.toString.call(d)); console.log(d instanceof RegExp); console.log(d instanceof Object); console.log(RegExp instanceof Object);
运行结果如下,可以看出正则也是Object

最最后还有个function
let d = function () {}; console.log(typeof d ); console.log(Object.prototype.toString.call(d)); console.log(d instanceof Function); console.log(d instanceof Object); console.log(Function instanceof Object);
运行结果如下,可以看出function也是Object


测试一圈回来发现,其实只要记住有八种数据类型,null也是数据类型就行了
三 .== 和 === 和 Object.is() 的区别
== 只比较值,如果类型不同,转换成相同的类型再比较,往String或者Number转
=== 类型和值都相等才相等
Object.is() 和===基本一样,只有下面这个区别
讯享网console.log(+0 === -0);//true console.log(NaN === NaN);//false console.log(Object.is(+0,-0));//false console.log(Object.is(NaN,NaN));//true
能把===用好就不错了
== 如果两边数据类型不同,转换规则如下:
1.布尔值===数据,则把布尔值转换为数值
2.字符串===数值,则通过Number()把字符串转为数值
3.如果一边是对象,则调用对象的valueof()方法后再比较
4.null和undefined相等
5.NaN和NaN不相等
6.如果两边都是对象,则比较他们是不是指向同一个对象

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/131637.html