粗粒度
===
typeof
返回字符串类型的结果值
缺点
无法具体区分出object类型
typeof 1 // 'number'typeof '1' // 'string'typeof undefined // 'undefined'typeof true // 'boolean'typeof Symbol() // 'symbol'typeof null // 'object'typeof [] // 'object'typeof {} // 'object'typeof console // 'object'typeof console.log // 'function'
instanceof
作用
判断前值是否是后值的构造函数生成的实例
let Car = function() {}let benz = new Car()benz instanceof Car // truelet car = new String('Mercedes Benz')car instanceof String // truelet str = 'Covid-19'str instanceof String // false"abc" instanceof String // false"Abc" instanceof Object // falsenull instanceof Object // false{} instanceof Object // Uncaught SyntaxError: Unexpected token 'instanceof'({}) instanceof Object // true[] instanceof Array // true[] instanceof Objcet // truevar b1 = {b2:[1,'abc',console.log],b3:function(){console.log(123)}}b1 instanceof Object // trueb1.b2 instanceof Object // trueb1.b2 instanceof Array // trueb1.b3 instanceof Function // trueb1.b3 instanceof Object // true
instanceof原理
https://www.yuque.com/docs/share/b45c44ca-626e-40dc-8be0-07abcb706ce9?#
手写instanceof
function myInstanceof(left, right) {// 这里先用typeof来判断基础数据类型,如果是,直接返回falseif(typeof left !== 'object' || left === null) return false;// getProtypeOf是Object对象自带的API,能够拿到参数的原型对象let proto = Object.getPrototypeOf(left);while(true) { //循环往下寻找,直到找到相同的原型对象if(proto === null) return false;if(proto === right.prototype) return true;//找到相同原型对象,返回trueproto = Object.getPrototypeof(proto);}}// 验证一下自己实现的myInstanceof是否OKconsole.log(myInstanceof(new Number(123), Number)); // trueconsole.log(myInstanceof(123, Number)); // false
细粒度
Array.isArray(value)
Object.prototype.toString
Object.prototype.toString({}) // "[object Object]"Object.prototype.toString.call({}) // 同上结果,加上call也okObject.prototype.toString.call(1) // "[object Number]"Object.prototype.toString.call('1') // "[object String]"Object.prototype.toString.call(true) // "[object Boolean]"Object.prototype.toString.call(function(){}) // "[object Function]"Object.prototype.toString.call(null) //"[object Null]"Object.prototype.toString.call(undefined) //"[object Undefined]"Object.prototype.toString.call(/123/g) //"[object RegExp]"Object.prototype.toString.call(new Date()) //"[object Date]"Object.prototype.toString.call([]) //"[object Array]"Object.prototype.toString.call(document) //"[object HTMLDocument]"Object.prototype.toString.call(window) //"[object Window]"
封装全类型检测函数
function getType(obj){let type = typeof obj;if (type !== "object") { // 先进行typeof判断,如果是基础数据类型,直接返回return type;}// 对于typeof返回结果是object的,再进行如下的判断,正则返回结果return Object.prototype.toString.call(obj).replace(/^\[object (\S+)\]$/, '$1'); // 注意正则中间有个空格}/* 代码验证,需要注意大小写,哪些是typeof判断,哪些是toString判断?思考下 */getType([]) // "Array" typeof []是object,因此toString返回getType('123') // "string" typeof 直接返回getType(window) // "Window" toString返回getType(null) // "Null"首字母大写,typeof null是object,需toString来判断getType(undefined) // "undefined" typeof 直接返回getType() // "undefined" typeof 直接返回getType(function(){}) // "function" typeof能判断,因此首字母小写getType(/123/g) //"RegExp" toString返回
