其中检测的方法有:
typeof、instanceof、constructor、Object.prototype.toString().call()、query.type()。
1. typeof检测分析
-
typeof 检测基本类型除了null和undefined以外,都会检测出对应的类型,
然而null检测出来的是object , undefined检测出来的只是undefined
此外,typeof如果检测引用类型中的function那么返回的类型还是function。 -
用 typeof 检测构造函数创建的Number,String,Boolean都返回object。
-
引用数据类型中的:Array,Object,Date,RegExp。不可以用typeof检测。都会返回小写的object。
console.log(
typeof 100, //"number"
typeof 'abc', //"string"
typeof false, //"boolean"
typeof undefined, //"undefined"
typeof null, //"object"
typeof [1,2,3], //"object"
typeof {a:1,b:2,c:3}, //"object"
typeof function(){console.log('aaa');}, //"function"
typeof new Date(), //"object"
typeof /^[a-zA-Z]{5,20}$/, //"object"
typeof new Error() //"object"
typeof new Number(100), //'object'
typeof new String('abc'),// 'string'
typeof new Boolean(true),//'boolean'
);
2. instanceof检测分析:
instanceof 检测基本数据类型,返回的是boolean类型中的false;如果检测基本数据类型中的null和undefined,返回的也是false,因为他们类型就是本身并不是Object创建出了它们。而检测引用数据类型返回的是boolean类型中的true。 然而字面量值不可以用instanceof检测,而构造函数创建的值可以。
console.log(
100 instanceof Number, //false
'dsfsf' instanceof String, //false
false instanceof Boolean, //false
undefined instanceof Object, //false
null instanceof Object, //false
[1,2,3] instanceof Array, //true
{a:1,b:2,c:3} instanceof Object, //true
function(){console.log('aaa');} instanceof Function, //true
new Date() instanceof Date, //true
/^[a-zA-Z]{5,20}$/ instanceof RegExp, //true
new Error() instanceof Error //true
)
3.Object.prototype.toString().call()
我们可以通过 toString() 来获取每个对象的类型,
为了每个对象都能通过Object.prototype.toString()来检测,
需要以 Function.prototype.call() 或 Function.prototype.apply() 的形式来调用,传递要检查的对象作为第一个参数。
var aa = Object.prototype.toString;
console.log(aa.call(123));//[object Number]
console.log(aa.call('123'));//[object String]
console.log(aa.call([1,2,3,4,5]));//[object Array]
console.log(aa.apply([1,2,3,4,5]));//[object Array]
console.log(aa.call({age:16}));//[object Object]
console.log(aa.call(undefined));//[object Undefined]
console.log(aa.call(null));//[object Null]
console.log(aa.apply(null));//[object Null]
console.log(aa.call(new Date()));//[object Date]
console.log(aa.call(/^[a-zA-Z]{5,20}$/));//[object RegExp]
console.log(aa.call(new Error()));//[object Error]
4. constructor
constructor是prototype对象上的属性,指向构造函数。根据实例对象寻找属性的顺序,
若实例对象上没有实例属性或方法时,就去原型链上寻找。实例对象也是能使用constructor属性的。
//实例代码展示:
console.log(new Number(666).constructor);
ƒ Number() { [native code] }
//构造函数展示:
var num = 123;
var str = 'abcdef';
var bool = true;
var arr = [1, 2, 3, 4];
var json = {name:'wenzi', age:25};
var func = function(){ console.log('this is function'); }
var und = undefined;
var nul = null;
var date = new Date();
var reg = /^[a-zA-Z]{5,20}$/;
var error= new Error();
function Person(){
}
var tom = new Person();
// undefined和null没有constructor属性
console.log(
tom.constructor==Person,
num.constructor==Number,
str.constructor==String,
bool.constructor==Boolean,
arr.constructor==Array,
json.constructor==Object,
func.constructor==Function,
date.constructor==Date,
reg.constructor==RegExp,
error.constructor==Error
);
//所有结果均为true
5. 万能的方法:jquery.type()
在判断前,需要我们先引入jquery插件,然后再操作进行数据判断其类型;
console.log($.type( undefined ) === "undefined");//true
console.log($.type(window.notDefined));//undefined
console.log($.type());//undefined
console.log($.type(null));//null
console.log($.type(3));//number
console.log($.type('6'));//string
console.log($.type([]));//array
console.log($.type(new Date()));//date
console.log($.type(new Error()));//error
console.log($.type(/test/));//regexp