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'); }
這樣判斷一個變量的數據類型就很方便了。