javascript基本数据类型有:String,Number,Boolean,Null,Undefined。
复杂数据类型:Object
新增:Symbol,BigInt
我们经常会判断数据返回,或者参数的数据类型,以下列四种举判断方法:typeof、instanceof、constructor、toString
我们先声明一些简单数据类型的变量:
let str = 'abcd';
let num = 123123;
let boo = true;
let und = undefined;
let nul = null;
let sym = Symbol('a');
let big = 9007199254740998n;
let arr = [123,123,1];
let obj = {a:1,b:2};
let reg = /abc/;
let fn = function(){console.log(666)};
let date = new Date();
1.typeof判断基本数据类型
typeof
操作符返回一个字符串,表示未经计算的操作数的类型
//字符串
typeof str; //'string'
//数值
typeof num; //'number'
//布尔值
typeof boo; //'boolean'
//undefined
typeof und; //'undefined'
//Symbol
typeof sym; //'symbol'
//BigInt
typeof big //'bigint'
//对象
typeof nul; //'object'
typeof arr; // 'object'
typeof obj; //'object'
typeof date; //'object'
typeof reg; //'object'
//函数
typeof fn; //'funciton'
typeof可以识别出基本数据类型,但是不能识别null,Array,Date,RegExp等都把它统一归为object类型 。
2.通过instanceof判断引用类型
instanceof
运算符用来检测 constructor.prototype
是否存在于参数 object
的原型链上。通俗来讲就是说instanceof判断该引用类型是否属于某个构造函数的实例。
//string
str instanceof String //false
//number
num instanceof Number //false
//boolean
boo instanceof Boolean //false
//null
nul instanceof Object; //false
//symbol
sym instanceof Symbol; //false
//bigInt
big instanceof BigInt; //false
//object
obj instanceof Object; //true
arr instanceof Object; //true
fn instanceof Object; //true
date instanceof Object; //true
reg instanceof Object; true
//function
fn instanceof Function; //true
//array
arr instanceof Array; //true
//data
date instanceof Date; //true
//RegExp
reg instanceof RegExp; true
String,Array
和 Date等
对象同时也属于Object
类型(他们是由 Object
类派生出来的)。
从结果中看出instanceof不能识别出基本的数据类型 number、boolean、string、undefined、null、Symbol,bigInt但是可以识别出Array、Object、Function,同时对于是使用new声明的类型,它还可以检测出多层继承关系
3.通过constructor来判断
constructor判断就是判断对象是否是构造函数的实例了
我们先来看看关于原型链的知识
function Person(){
}
person1 = new Person();
console.log(Person === Person.prototype.constructor); // true
console.log(person.__proto__ == Person.prototype) // true
console.log(Person.prototype.constructor === person1.__proto__.constructor)//true
console.log(person1.constructor === Person);
所以我们可以使用constructor来判断数据类型:
//string
str.constructor === String //true
//number
num.constructor === Number //true
//boolean
boo.constructor === Boollean //true
//symbol
sym.constructor === Symbol //true
//big
big.constructor === BigInt //true
//object
obj.constructor === Object //true
//fn
fn.constructor === Function //true
//arr
arr.constructor === Array //true
//reg
reg.constructor === RegExp //true
//date
date.constructor === Date //true
说明,这里不像instanceof,fn,reg,date,arr都不是Object的直接实例
fn.constructor === Object //false
arr.constructor === Object //false
date.constructor === Object //false
reg.constructor === Object //false
//....
null、undefined没有construstor方法,因此constructor不能判断undefined和null,并且它是不安全的,因为contructor的指向是可以被改变
4.通过Object.prototype.toString来判断最准确最常用
每个对象都有一个 toString()
方法,当该对象被表示为一个文本值时,或者一个对象以预期的字符串方式引用时自动调用。默认情况下,toString()
方法被每个 Object
对象继承。如果此方法在自定义对象中未被覆盖,toString()
返回 "[object type]",其中 type
是对象的类型。以下代码说明了这一点:
var o = new Object();
o.toString(); // returns [object Object]
可以通过 toString()
来获取每个对象的类型。为了每个对象都能通过 Object.prototype.toString()
来检测,需要以 Function.prototype.call()
或者 Function.prototype.apply()
的形式来调用,传递要检查的对象作为第一个参数,称为 thisArg
。
下面这样的写法是为了判断一个变量是什么类型的数据:
var toString = Object.prototype.toString;
toString.call(123); //"[object Number]"
toString.call('abcdef'); //"[object String]"
toString.call(true); //"[object Boolean]"
toString.call([1, 2, 3, 4]); //"[object Array]"
toString.call({name:'wenzi', age:25}); //"[object Object]"
toString.call(function(){ console.log('this is function'); }); //"[object Function]"
toString.call(undefined); //"[object Undefined]"
toString.call(null); //"[object Null]"
toString.call(new Date()); //"[object Date]"
toString.call(/^[a-zA-Z]{5,20}$/); //"[object RegExp]"
toString.call(new Error()); //"[object Error]"
可以较全的判断js的数据类型