js判断数据类型的方法
js判断数据类型的方法(常见的六种)
1.最常见的判断方法:typeof
2.已知对象类型: instanceof
3.对象原型链判断方法: prototype 通用但很繁琐
4.根据对象的构造器constructor进行判断
5.jQuery方法: jquery.type()
6.严格运算符: ===
一、typeof
console.log(typeof "helloworld");
console.log(typeof 12);
console.log(typeof [1, 2, 3]);
console.log(typeof new Function());
console.log(typeof new Date());
console.log(typeof new RegExp());
console.log(typeof Symbol());
console.log(typeof true);
console.log(typeof null);
console.log(typeof undefined);
二.instance of
判断是否为object(对象类型)基本数据类型不是,引用数据类型是
console.log(phone instanceof Object);
console.log(document.getElementsByTagName('div')[0] instanceof Object);
console.log(window instanceof Object);
var obj = {}
console.log(obj instanceof Object);
console.log(new Date() instanceof Object);
var arr = []
console.log(arr instanceof Object);
var fun = function () { }
console.log(fun instanceof Object);
var num = 10
var str = '哈哈'
console.log(num instanceof Object);
console.log(str instanceof Object);
console.log(null instanceof Object);
console.log(undefined instanceof Object);
三.对象原型链判断方法: Object.prototype.toString.call()
适用于所有类型的判断检测,注意区分大小写. toString方法,在Object原型上返回数据格式,
console.log(Object.prototype.toString.call("123"))
console.log(Object.prototype.toString.call(123))
console.log(Object.prototype.toString.call(true))
console.log(Object.prototype.toString.call([1, 2, 3]))
console.log(Object.prototype.toString.call(null))
console.log(Object.prototype.toString.call(undefined))
console.log(Object.prototype.toString.call({ name: 'Hello' }))
console.log(Object.prototype.toString.call(function () { }))
console.log(Object.prototype.toString.call(new Date()))
console.log(Object.prototype.toString.call(/\d/))
console.log(Object.prototype.toString.call(Symbol()))
四.根据对象的constructor进行判断
constructor 判断方法跟instanceof相似,但是constructor检测Object与instanceof不一样,constructor还可以处理基本数据类型的检测,不仅仅是对象类型
注意:
1.null和undefined没有constructor;
2.判断数字时使用(),比如 (123).constructor,如果写成123.constructor会报错
3.constructor在类继承时会出错,因为Object被覆盖掉了,检测结果就不对了
// 1、DOM对象
console.log(document.getElementsByTagName('div')[0].constructor); // HTMLDivElement() { [native code] }
// 2、BOM对象
console.log(history.constructor); // History() { [native code] }
// 3、自定义对象
var obj = {}
console.log(obj.constructor); // Object() { [native code] }
// 4、内置对象
console.log(new Date().constructor); // Date() { [native code] }
// 5、数组
var arr = []
console.log(arr.constructor); // Array() { [native code] }
// 6、函数
var fun = function () {
}
// 判断数据类型 constructor
console.log(fun.constructor); // Function() { [native code] }
var num = 10
console.log(num.constructor == Object); // false
五、jQuery方法: jquery.type()
据说是无敌万能的方法.如果对象是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
六.有局限的判断:严格运算符===
通常===出现在我们的条件判断中,比如判断一个变量是否为空,变量是否为数据等,示例如下
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()