(8种)数据类型
数据类型文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures
分类:基本类型 primitives、Object类型
- 基本类型(基本数值、基本数据类型)是一种既非对象也无方法的数据。在 JavaScript 中,共有7种基本类型:String,Number,BigInt,Boolean,Null,Undefined,Symbol (ECMAScript 2016新增)。详情查看MDN文档
- Object类型包括:函数Function:特别的对象(可以执行)、数组Array:特别的对象(数值下标、内部数据有序)以及特殊的正则(RegExp)和日期(Date)。
BigInt类型: 属于基础数值类型,可以存储和操作操作大整数,甚至可以超过数字的安全整数限制,通过在整数末尾附加 n 或调用构造函数来创建的BigInt类型的数据。
判断数据类型
方法:1、typeof 2、instance of 3、=== 4、Object.prototype.toString.call
- typeof:(除了null、Array、Reg、Date外(这几个使用typeof 返回值都是 ‘object’)其他都能判断)可以判断 undefined、number、string、boolean、bigint、symbol,typeof 也可以判断 object 和 function 类型,不能用来判断区别 null 与 object ,以及 object 和Array 。
- instanceof:判断对象 Object 的具体类型,A instanceof B 判断A是否是B构造函数的实例,返回值为boolean
- === :可以判断 null 、undefined
- Object.prototype.toString.call:可以判断某个对象属于哪种内置类型,toString是Object原型对象上的一个方法,该方法默认返回其调用者的具体类型,是 toString运行时this指向的对象类型, 返回的类型格式为[object,xxx],用call来强制执行Object的toString方法。
注意:typeof 返回的是数据类型的字符串表达
/*
* typeof:可以判断 undefined/number/string/boolean/(null除外,typeof null===“object”)typeof也可以判断object和function类型
* instanceof:判断对象Object的具体类型,A instanceof B 判断A是否是B构造函数的实例,返回值为boolean
* === :可以判断null/undefined
*/
//typeof 返回的是数据类型的字符串表达
var a
console.log(a,typeof a,typeof a==='undefined',a===undefined) //undefined 'undefined' true true
console.log(undefined==='undefined') //false
a=3
console.log(typeof a==='number') //true
a='ljy'
console.log(typeof a==='string') //true
a=true
console.log(typeof a==='boolean')//true
var a=123n;
console.log(typeof a==='bigint')//true
const symbol1 = Symbol();
console.log(typeof symbol1==='symbol')//true
a=null
console.log(typeof a,a===null)//object true(所以typeof不能用来判断null类型)
//Object类型-------------------------------------
var b1={
b2:[123,'ljy',console.log],
b3:function () {
console.log('function b3')
}
}
console.log(b1 instanceof Object,b1 instanceof Array) //true false
console.log(b1.b2 instanceof Object,b1.b2 instanceof Array) //true true
console.log(b1.b3 instanceof Object,b1.b3 instanceof Function)//true true
//typeof也可以判断object和function类型,array不可以
console.log(typeof b1,typeof b1.b2,typeof b1.b3)//object object function
console.log(typeof b1==='object',typeof b1.b2==='array',typeof b1.b3==='function')//true false true
console.log(typeof b1.b2[2])//function
使用Object.prototype.toString.call方法来判断数据类型:
Object.prototype.toString.call(null); // "[object Null]"
Object.prototype.toString.call(undefined); // "[object Undefined]"
Object.prototype.toString.call(“abc”);// "[object String]"
Object.prototype.toString.call(123);// "[object Number]"
Object.prototype.toString.call(true);// "[object Boolean]"
//*************************函数类型**************************
Function fn(){
console.log(“test”);
}
Object.prototype.toString.call(fn); // "[object Function]"
//*************************日期类型**************************
var date = new Date();
console.log(Object.prototype.toString.call(date));// "[object Date]"
//*************************正则表达式**************************
var reg = /[hbc]at/gi;
console.log(Object.prototype.toString.call(reg)); // "[object RegExp]"
判断自定义类型:
如判断person是Person类创建的实例,只能用instanceof 操作符来进行判断:
// 实例:实例对象
// 类型:类型对象
function Person(name,age) {//创建构造函数 类型
this.name=name;
this.age=age;
}
var person=new Person('tony',15);//根据类型创建的实例对象
console.log(typeof person);//object
console.log(Object.prototype.toString.call(person));//[object Object]
console.log(person instanceof Person);//true