typeofchrome
若是使用typeof来判断数据类型的话,结果以下:json
console.log( typeof 123, //"number" typeof 'dsfsf', //"string" typeof false, //"boolean" typeof [1,2,3], //"object" typeof {a:1,b:2,c:3}, //"object" typeof function(){console.log('aaa');}, //"function" typeof undefined, //"undefined" typeof null, //"object" typeof new Date(), //"object" typeof /^[a-zA-Z]{5,20}$/, //"object" typeof new Error() //"object" );
以上结果都是在chrome浏览器里运行结果,能够发现以下规律浏览器
Array,Object,null,Date,RegExp,Error这几个类型都被typeof判断为object,因此若是想要判断这几种类型,就不能使用typeof了。app
Number,String,Boolean,Function,undefined,若是想判断这几种类型,那就可使用typeof。函数
instanceofthis
除了使用typeof来判断,还可使用instanceof。instanceof运算符须要指定一个构造函数,或者说指定一个特定的类型,它用来判断这个构造函数的原型是否在给定对象的原型链上。spa
结果以下:prototype
console.log( 123 instanceof Number, //false 'dsfsf' instanceof String, //false false instanceof Boolean, //false [1,2,3] instanceof Array, //true {a:1,b:2,c:3} instanceof Object, //true function(){console.log('aaa');} instanceof Function, //true undefined instanceof Object, //false null instanceof Object, //false new Date() instanceof Date, //true /^[a-zA-Z]{5,20}$/ instanceof RegExp, //true new Error() instanceof Error //true )
能够发现以下规律:code
Number,String,Boolean没有检测出他们的类型,可是若是使用下面的写法则能够检测出来:对象
var num = new Number(123); var str = new String('dsfsf'); var boolean = new Boolean(false);
还须要注意null和undefined都返回了false,这是由于它们的类型就是本身自己,并非Object建立出来它们,因此返回了false。
constructor
constructor是prototype对象上的属性,指向构造函数。根据实例对象寻找属性的顺序,若实例对象上没有实例属性或方法时,就去原型链上寻找,所以,实例对象也是能使用constructor属性的。
若是输出一个类型的实例的constructor,就以下所示:
console.log(new Number(123).constructor) //ƒ Number() { [native code] }
能够看到它指向了Number的构造函数,所以,可使用num.constructor==Number来判断一个变量是否是Number类型的。
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
除了undefined和null以外,其余类型均可以经过constructor属性来判断类型。
使用toString()检测对象类型
能够经过toString() 来获取每一个对象的类型。为了每一个对象都能经过 Object.prototype.toString() 来检测,须要以 Function.prototype.call() 或者 Function.prototype.apply() 的形式来调用,传递要检查的对象做为第一个参数,称为thisArg。
var toString = Object.prototype.toString; toString.call(123); //"[object Number]" toString.call('abcdef'); //"[object String]" toString.call(true); //"[object Boolean]" toString.call([1, 2, 3, 4]); //"[object Array]" toString.call({name:'wenzi', age:25}); //"[object Object]" toString.call(function(){ console.log('this is function'); }); //"[object Function]" toString.call(undefined); //"[object Undefined]" toString.call(null); //"[object Null]" toString.call(new Date()); //"[object Date]" toString.call(/^[a-zA-Z]{5,20}$/); //"[object RegExp]" toString.call(new Error()); //"[object Error]"
这样能够看到使用Object.prototype.toString.call()的方式来判断一个变量的类型是最准确的方法。
封装一个获取变量准确类型的函数
function gettype(obj) { var type = typeof obj; if (type !== 'object') { return type; } //若是不是object类型的数据,直接用typeof就能判断出来 //若是是object类型数据,准确判断类型必须使用Object.prototype.toString.call(obj)的方式才能判断 return Object.prototype.toString.call(obj).replace(/^\[object (\S+)\]$/, '$1'); }
这样判断一个变量的数据类型就很方便了。