js的对象类型主要分为两大类:基本数据类型(原生类型)、包装数据类型(包装类型);
1、基本数据类型
typeof 123; // 'number'
typeof NaN; // 'number'
typeof 'str'; // 'string'
typeof true; // 'boolean'
typeof undefined; // 'undefined'
typeof Math.abs; // 'function'
typeof null; // 'object'
typeof []; // 'object'
typeof {}; // 'object'
console.log("typeof 123= ",typeof 123);
console.log("typeof NaN= ",typeof NaN);
console.log("typeof 'str'= ",typeof 'str');
console.log("typeof true= ",typeof true);
console.log("typeof undefined= ",typeof undefined);
console.log("typeof Math.abs= ",typeof Math.abs);
console.log("typeof null= ",typeof null);
console.log("typeof []= ",typeof []);
console.log("typeof {}= ",typeof {});
效果如下:
![](https://img-blog.csdnimg.cn/20210209095653502.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NhbnFpbWE=,size_16,color_FFFFFF,t_70)
2、包装数据类型
var a1 = new Number(123) //object
var b1 = new Boolean(true) //object
var c1 = new String('hello') //object
console.log("new Number(123)= ",typeof a1)
console.log("new Boolean(true)= ",typeof b1)
console.log("new String('hello')= ",typeof c1)
效果如下:
![](https://img-blog.csdnimg.cn/20210209100944272.png)
包装数据类型,是用new func()得到的,它是一个object,与基本数据类型不相同,所以包装数据类型与基本数据类型,不能用 === 来判断相等。
3、案例分析
3.1 将基本数据类型123,与包装数据类型123,进行比较
var a1 = 123; //基本数据类型:123, 属于number
var b1 = new Number(123); //包装数据类型:123, 属于object
var bRet = (a1 === b1);
console.log("check a1,b1",bRet)
比较结果:
3.2 将基本数据类型123,与原生数据123,进行比较
var a1 = 123; //基本数据类型:123, 属于number
var c1 = Number(123); //原生数据:123, 属于number
var bRet = (a1 === c1);
console.log("check a1,c1",bRet)
比较结果:
3.3 小结
带new生成的数据为包装类型(包装数据类型),不带new生成的数据为原生类型(基本数据类型)。