结论:contructor和Object.prototype.toString.call可以用于全部情况。typeof更可用于检测基本数据类型(除了null),instanceof用于检测引用数据类型。
- constructor:
- 格式 数据.constructor == 数据类型
-
console.log((1).constructor===Number) console.log(('string').constructor===String) console.log((true).constructor===Boolean) console.log((function(){}).constructor===Function) console.log((Symbol('a')).constructor===Symbol) console.log(({}).constructor===Object) console.log(([]).constructor===Array) console.log((/a/).constructor===RegExp) console.log((new Date()).constructor===Date) console.log((new Error()).constructor===Error) console.log((new Set()).constructor===Set) console.log((new Map()).constructor===Map) console.log((new WeakSet()).constructor===WeakSet) console.log((new WeakMap()).constructor===WeakMap)//全为true
-
typeof:
typeof
操作符是按照数据在计算机底层存储的二进制结果来进行检测的,该方式无法区分数组、对象、null。(typeof null
的结果是"Object"
是由于null
在底层中存储的二进制是:000000
,对象在底层存储的二进制是000xxx
;所以使用typeof
检测null
的时候会打印出"Object")
-
console.log(typeof 1)//number console.log(typeof 'string')//string console.log(typeof true)//boolean console.log(typeof function(){})//function console.log(typeof Symbol('a'))//symbol console.log(typeof {})//object console.log(typeof [])//object console.log(typeof /a/)//object console.log(typeof new Date())//object console.log(typeof new Error())//object console.log(typeof new Set())//object console.log(typeof new Map())//object console.log(typeof new WeakSet())//object console.log(typeof new WeakMap())//object console.log(typeof null)//object console.log(typeof undefined)//undefined
-
-
instanceof:根据该实例对象的构造函数是否等于后面一个构造函数,只用于判断引用类型,基础数据类型不是对象,所以无法使用该种方法判断
-
console.log(1 instanceof Number)//false console.log('string' instanceof String)//false console.log(true instanceof Boolean)//false console.log(function(){} instanceof Function)//true console.log(Symbol('a') instanceof Symbol)//false console.log({} instanceof Object)//true console.log([] instanceof Array)//true console.log(/a/ instanceof RegExp)//true console.log(new Date() instanceof Date)//true console.log(new Error() instanceof Error)//true console.log(new Set() instanceof Set)//true console.log(new Map() instanceof Map)// true console.log(new WeakSet() instanceof WeakSet)//true console.log(new WeakMap() instanceof WeakMap)//true console.log(null instanceof Object)//false console.log(undefined instanceof Object)//false console.log(new Number(1) instanceof Number)//true console.log(new String('string') instanceof String)//true console.log(new Boolean(true) instanceof Boolean)//true
-
-
Object.prototype.toString.call:通过原型链访问实例对象指向的构造函数名(所有 typeof 返回值为 "object" 的对象(如数组)都包含一个内部属性 [[Class]],通过上述方法访问对象的内部属性进行判断)
-
console.log(Object.prototype.toString.call(1)==='[object Number]') console.log(Object.prototype.toString.call('string')==='[object String]') console.log(Object.prototype.toString.call(true)==='[object Boolean]') console.log(Object.prototype.toString.call(function(){})==='[object Function]') console.log(Object.prototype.toString.call(Symbol('a'))==='[object Symbol]') console.log(Object.prototype.toString.call({})==='[object Object]') console.log(Object.prototype.toString.call([])==='[object Array]') console.log(Object.prototype.toString.call(/a/)==='[object RegExp]') console.log(Object.prototype.toString.call(new Date())==='[object Date]') console.log(Object.prototype.toString.call(new Error())==='[object Error]') console.log(Object.prototype.toString.call(new Set())==='[object Set]') console.log(Object.prototype.toString.call(new Map())==='[object Map]') console.log(Object.prototype.toString.call(new WeakSet())==='[object WeakSet]') console.log(Object.prototype.toString.call(new WeakMap())==='[object WeakMap]') console.log(Object.prototype.toString.call(null)==='[object Null]') console.log(Object.prototype.toString.call(undefined)==='[object Undefined]')//全为true
-