一、typeof
1. typeof
运算符
返回字符串
用于判断简单数据类型
可能的返回值:undefined、boolean、number、string、object、function、bigint、symbol
特殊及原因:
// null:全零(对象);undefined:-2^30整数
// 000对象;010浮点数;100字符串;110布尔;1整数
console.log(typeof null)//object
console.log(typeof NaN)//number
2. 检测数据类型:
console.log(typeof 1)//number
console.log(typeof '1')//string
console.log(typeof [1])//object
console.log(typeof undefined)//undefined
console.log(typeof true)//boolean
console.log(typeof (()=> {}))//function
console.log(typeof {})//object
console.log(typeof BigInt(111))//bigint
console.log(typeof Symbol(1))//symbol
3. 优化typeof
function typeOf(obj) {
if (typeof obj == 'object') {
return Object.prototype.toString.call(obj).slice(8,-1).toLowerCase()//"[object Array]"
} else {
return typeof obj
}
}
测试:
console.log(typeOf(1))//number
console.log(typeOf('1'))//string
console.log(typeOf([1]))//object=>array
console.log(typeOf( undefined))//undefined
console.log(typeOf(true))//boolean
console.log(typeOf(()=> {}))//function
console.log(typeOf({}))//object
console.log(typeOf(BigInt(111)))//bigint
console.log(typeOf(Symbol(1)))//symbol
console.log(typeOf(null))//object=>null
console.log(typeOf( NaN))//number
二、instanceof
1. instanceof
作用:检测某个对象(引用类型)是否属于某个类型
场景:检测(引用)对象的类型,基本类型的检测用typeof
语法:object(要检测的对象) instanceof constructor(某个构造函数)
本质:instanceof 运算符用于检测 constructor.prototype
是否存在于参数object的原型链上, 如果存在,返回true,否则返回false
2.测试instance
// instanceof测试 返回true or false 检测原型链上是否存在该构造函数
// 检测普通数据类型
console.log((1) instanceof Number) // false
console.log((NaN) instanceof Number)//false
console.log(true instanceof Boolean)// false
console.log('sss' instanceof String)//false
// console.log(undefined instanceof undefined)//报错
console.log(null instanceof Object)//false
console.log(BigInt(111) instanceof BigInt)//false
console.log(Symbol(12) instanceof Symbol)//false
// 检测引用数据类型
console.log((() => { }) instanceof Function)//true
console.log(new Date(100) instanceof Date)//true....
console.log({} instanceof null)//报错
3.手写instanceof:myinstanceOf
function myinstanceOf(data, type) {
// data为undefined,type为null 报错
if (type === null || typeof data === 'undefined') throw new Error('错误')
// 除过 data为一个函数类型,
// 基本数据类型 返回false
if (typeof data !== 'function' && typeof data !== 'object' ) return false
let point = data
while (point) {
// 找到顶端=>没找到
if (point === null)
return false
// 找到了
if (point === type.prototype)
return true
// 沿原型链找
point = point.__proto__
}
// data 为null
return false
}
4.测试myinstanceOf代码:
console.log(myinstanceOf(1,Number)) // false
console.log(myinstanceOf(NaN,Number))//false
console.log(myinstanceOf( true,Boolean))// false
console.log(myinstanceOf('sss' , String))//false
// console.log(myinstanceOf(undefined, undefined))//报错
console.log(myinstanceOf(null , Object))//false
console.log(myinstanceOf(BigInt(111) , BigInt))//false
console.log(myinstanceOf(Symbol(12) , Symbol))//false
// 检测引用数据类型
console.log(myinstanceOf((() => { }), Function))//true
console.log(myinstanceOf((function() { }) , Function))//true
console.log(myinstanceOf(new Date(100) , Date))//true....
console.log(myinstanceOf([1,2] , Array))//true
// console.log(myinstanceOf({} , null))//报错
遗留问题:
Object.prototype.toString