目录
1. typeof {}
2. {} instanceof Object
3. {}.constructor === Object
4. Object.property.toString.call({})
内容:
1. typeof {}
注意:能够区分string、number、boolean、undefined、function,无法区分array、null、object
??//typeof ???console.log(typeof ""); ???????????//string ???console.log(typeof 1); ????????????//number ???console.log(typeof true); ?????????//boolean ???console.log(typeof null); ?????????//object ???console.log(typeof undefined); ????//undefined ???console.log(typeof []); ???????????//object ???console.log(typeof function(){}); ?//function ???console.log(typeof {}); ???????????//object
2. {} instanceof Object
注意:能法区分array、function、object,无法区分字面量string、number、boolean,报错null、undefined
//instanceof ???console.log("1" instanceof String); ????????????????//false ???console.log(1 instanceof Number); ??????????????????//false ???console.log(true instanceof Boolean); ???????????????//false ??????// console.log(null instanceof Null); ????????????//报错 Null is not defined ?????// console.log(undefined instanceof Undefined); ???// 报错 Undefined is not defined ???console.log([] instanceof Array); ???????????????????//true ???console.log(function(){} instanceof Function); ??????//true ???console.log({} instanceof Object); ??????????????????//true
注意:能够区分new生产的string、number、boolean
???console.log(new String(‘‘) instanceof String); ????????????????//true ???console.log(new Number(1) instanceof Number); ??????????????????//true ???console.log(new Boolean(true) instanceof Boolean); ???????????????//true
3 {}.constructor === Object
注意:constructor是判断一个元素是否在另一个元素的原型链上面,报错null、undefined
//constructor ???console.log(("1").constructor === String); ???????????????????//true ???console.log((1).constructor === Number); ?????????????????????//true ???console.log((true).constructor === Boolean); ?????????????????//true ???// console.log((null).constructor === Null); ??????????????????//报错 Cannot read property ‘constructor‘ of null ???// console.log((undefined).constructor === Undefined); ????????//报错 Cannot read property ‘constructor‘ of undefined ???console.log(([]).constructor === Array); ?????????????????????//true ???console.log((function() {}).constructor === Function); ???????//true ???console.log(({}).constructor === Object); ????????????????????//true
注意:如果是在原型链上面,会出现问题
???function Fn(){}; ???Fn.prototype=new Array(); ???var f=new Fn(); ???console.log(f.constructor===Fn); ??????????????????//false ???console.log(f.constructor===Array); ???????????????//true
4. Object.property.toString.call({})
注意:jquery ?也是用这个方法进行数据类型检测的
//Object.prototype.toString ???var a = Object.prototype.toString; ???console.log(a.call("aaa")); ????????????????????//[object String] ???console.log(a.call(1)); ????????????????????????//[object Number] ???console.log(a.call(true)); ?????????????????????//[object Boolean] ???console.log(a.call(null)); ?????????????????????//[object Null] ???console.log(a.call(undefined)); ????????????????//[object Undefined] ???console.log(a.call([])); ???????????????????????//[object Array] ???console.log(a.call(function() {})); ????????????//[object Function] ???console.log(a.call({})); ???????????????????????//[object Object]
4. jQuery方法
以下方法对参数进行判断,返回一个布尔值。
jQuery.isArray():是否为数组。
jQuery.isEmptyObject():是否为空对象(不含可枚举的属性)。
jQuery.isFunction():是否为函数。
jQuery.isNumeric():是否为数字。
jQuery.isPlainObject():是否为使用“{}”或“new Object”生成的对象,而不是浏览器原生提供的对象。
jQuery.isWindow():是否为window对象。
jQuery.isXMLDoc():判断一个DOM节点是否处于XML文档之中。
js数据类型检测
原文地址:https://www.cnblogs.com/shaokevin/p/9633232.html