- 1.typeof 语法:typeof 数据类型
简单数据类型:除了null其余都能判断出来
1.consolog(
typeof 12 //Number
typeof'12' //String
typeof undefined
typeof null object
typeof boolean布尔值(不灵)
typeof Symbol //Symbol
)
复合数据类型:除了funtin其余都判断为object
2.consolog(
typeof function //function
typeof {} //Object
typeof [] //Object
)
优点:简单数据类型除null其余都能判断除来
缺点:复合数据类型除function取余都不能判断都为Object
-
- instanceof
语法:左实例instanceof右构造函数
作用:判断左边实例 是否和右边构造函数同一条原型链上
原理:实例化.proto ==构造函数.prototype说明是一条原型链上
1.简单数据类型:除了null其余都失效
缺点:类型申明只要不是构造函数(new)声明方式,instanceof将失效
优点:复合类型能判断
let obj = new Object();
let arr = new Array();
let date = new Date();
let regexp = new RegExp();
console.log(
obj instanceof Object, //truan 说明左实例和右构造函数同一条原型链上
arr instanceof Array, //truan
date instanceof Date, //truan
regexp instanceof RegExp //truan
);
总结: typeof和instanceof各有所长,刚好互补