js判断数据类型常用的6种方法 | 您所在的位置:网站首页 › 六种说明方法的意思是什么呢 › js判断数据类型常用的6种方法 |
js判断数据类型常用的方法,不论在开发中开始在面试中,都是经常遇到的问题,尤其是在面试时,当面试官问及js判断数据类型的方法时,回答的越多,说明掌握的广度跟深度越多,感觉自己逼格也越高.废话不多说了,进入正题 常见的判断js数据类型的方法有如下几种 1.最常见的判断方法:typeof 2.已知对象类型: instanceof 3.对象原型链判断方法: prototype 通用但很繁琐 4.根据对象的构造器constructor进行判断 5.jQuery方法: jquery.type() 6.严格运算符: === 下面依次说下每个方法的写法跟结果 一.typeof其中typeof返回的类型都是字符串形式,需注意!!!!! alert(typeof "helloworld") ------------------>"string" alert(typeof 123) ------------------>"number" alert(typeof [1,2,3]) ------------------>"object" alert(typeof new Function()) ------------------>"function" alert(typeof new Date()) ------------------>"object" alert(typeof new RegExp()) ------------------>"object" alert(typeof Symbol()) ------------------>"symbol" alert(typeof true) ------------------>"true" alert(typeof null) ------------------>"object" alert(typeof undefined) ------------------>"undefined" alert(typeof 'undefined') ------------------>"string"
注意:instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。 [1,2,3] instanceof Array -------->true new Date() instanceof Date -------->true new Function() instanceof Function -------->true new Function() instanceof function -------->false null instanceof Object -------->false适用于所有类型的判断检测,注意区分大小写. toString方法,在Object原型上返回数据格式, console.log(Object.prototype.toString.call("123")) -------->[object String] console.log(Object.prototype.toString.call(123)) -------->[object Number] console.log(Object.prototype.toString.call(true)) -------->[object Boolean] console.log(Object.prototype.toString.call([1, 2, 3])) -------->[object Array] console.log(Object.prototype.toString.call(null)) -------->[object Null] console.log(Object.prototype.toString.call(undefined)) -------->[object Undefined] console.log(Object.prototype.toString.call({name: 'Hello'})) -------->[object Object] console.log(Object.prototype.toString.call(function () {})) -------->[object Function] console.log(Object.prototype.toString.call(new Date())) -------->[object Date] console.log(Object.prototype.toString.call(/\d/)) -------->[object RegExp] console.log(Object.prototype.toString.call(Symbol())) -------->[object Symbol] 四.根据对象的constructor进行判断constructor 判断方法跟instanceof相似,但是constructor检测Object与instanceof不一样,constructor还可以处理基本数据类型的检测,不仅仅是对象类型 注意: 1.null和undefined没有constructor; 2.判断数字时使用(),比如 (123).constructor,如果写成123.constructor会报错 3.constructor在类继承时会出错,因为Object被覆盖掉了,检测结果就不对了 //注意当出现继承的时候,使用constructor会出现问题 function A() {}; function B() {}; A.prototype = new B(); //A继承自B console.log(A.constructor === B) -------->false var C = new A(); //现在开始判断C是否跟A的构造器一样 console.log(C.constructor === B) -------->true console.log(C.constructor === A) -------->false //解决这种情况,通常是手动调整对象的constructor指向 C.constructor = A; //将自己的类赋值给对象的constructor属性 console.log(C.constructor === A); -------->true console.log(C.constructor === B); -------->false据说是无敌万能的方法.如果对象是null跟undefined,直接返回"null"和"undefined", 注意:在使用时,一定要引入jquery文件,不然会报错,jQuery is not defined console.log(jQuery.type(undefined) === "undefined") -------->true console.log(jQuery.type() === "undefined"); -------->true console.log(jQuery.type(window.notDefined) === "undefined") -------->true console.log(jQuery.type(123) === "number") -------->true console.log(jQuery.type('123') === "string") -------->true console.log(jQuery.type([]) === "array") -------->true console.log(jQuery.type(true) === "boolean") -------->true console.log(jQuery.type(function(){}) === "function") -------->rue console.log(jQuery.type(new Date()) === "date") -------->true console.log(jQuery.type(/\d/) === "regexp") -------->true console.log(jQuery.type(new Error()) === "error") -------->true jq版本高于1.9.3 console.log(jQuery.type({name:'Hello'}) === "object") ------->true console.log(jQuery.type(Symbol()) === "symbol") ------->true ------->其余对象类型一般返回object 六.有局限的判断:严格运算符===通常===出现在我们的条件判断中,比如判断一个变量是否为空,变量是否为数据等,示例如下 var a = null; typeof a //object a === null //true /*扩展补充*/ //判断一个非数组变量是否为空 if(typeof a != 'undefined' && a ){} //判断一个数组变量是否为空 if (typeof a != “undefined” && a && a.length > 0) {}总之: 一般变量用typeof, 已知对象类型用instanceof, 通用方法Object.prototype.toString.call() jQuery项目万能方法jQuery.type() 欢迎大家批评指正 |
CopyRight 2018-2019 实验室设备网 版权所有 |