(面试题)数据类型判断方法

82 阅读1分钟

1.typeof:number、boolran、string、object(数组、对象、null)、function、undefined 方式:typeof 数据 2.instanceof:返回true或者false 限制:只能判断引用数据类型(数组/方法/对象),不能判断基本数据类型(数字/布尔/字符串:判断出来是错的) 方式:数据 instanceof Number/Boolean/String/Array/Function/Object 原理:判断数据原型链 有没有 该类型原型 3.constructor:返回true或者false(伪) 方式:(数据).constructor === Number/Boolean/String/Array/Function/Object 限制:(1)不能检测null和undefined (2)如果其构造函数显式原型改变,则实例函数用此方法检测出的数据类型 是 构造函数显式原型改变那个类型(若改变为new Array,则测出为Array) 4.Object.prototype.toString.call:返回 [object Number/Boolean/String/Array/Function/Object/null/undefined] 方式:Object.prototype.toString.call(数据)

image.png