判断数据类型
typeof
如果使用typeof來(lái)判斷數(shù)據(jù)類型的話,結(jié)果如下:
var num = new Number('123'); var str = new String('1223'); var bool = new Boolean(false); console.log(typeof 123, //"number"typeof num, //"object"
typeof 'dsfsf', //"string"
typeof str,//"object"
typeof false, //"boolean"
typeof bool,//"object"
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" );
以上結(jié)果都是在chrome瀏覽器里運(yùn)行結(jié)果,可以發(fā)現(xiàn)如下規(guī)律
Array,Object,null,Date,RegExp,Error這幾個(gè)類型都被typeof判斷為object,所以如果想要判斷這幾種類型,就不能使用typeof了。
Number,String,Boolean,Function,undefined,如果想判斷這幾種類型,那就可以使用typeof。
instanceof
除了使用typeof來(lái)判斷,還可以使用instanceof。instanceof運(yùn)算符需要指定一個(gè)構(gòu)造函數(shù),或者說(shuō)指定一個(gè)特定的類型,它用來(lái)判斷這個(gè)構(gòu)造函數(shù)的原型是否在給定對(duì)象的原型鏈上。
結(jié)果如下:
console.log(123 instanceof Number, //false'dsfsf' instanceof String, //falsefalse instanceof Boolean, //false[1,2,3] instanceof Array, //true{a:1,b:2,c:3} instanceof Object, //truefunction(){console.log('aaa');} instanceof Function, //trueundefined instanceof Object, //falsenull instanceof Object, //falsenew Date() instanceof Date, //true/^[a-zA-Z]{5,20}$/ instanceof RegExp, //truenew Error() instanceof Error //true )可以發(fā)現(xiàn)如下規(guī)律:
Number,String,Boolean沒(méi)有檢測(cè)出他們的類型,但是如果使用下面的寫法則可以檢測(cè)出來(lái):
var num = new Number(123); var str = new String('dsfsf'); var boolean = new Boolean(false);還需要注意null和undefined都返回了false,這是因?yàn)樗鼈兊念愋途褪亲约罕旧?#xff0c;并不是Object創(chuàng)建出來(lái)它們,所以返回了false。
constructor
constructor是prototype對(duì)象上的屬性,指向構(gòu)造函數(shù)。根據(jù)實(shí)例對(duì)象尋找屬性的順序,若實(shí)例對(duì)象上沒(méi)有實(shí)例屬性或方法時(shí),就去原型鏈上尋找,因此,實(shí)例對(duì)象也是能使用constructor屬性的。
如果輸出一個(gè)類型的實(shí)例的constructor,就如下所示:
console.log(new Number(123).constructor) //? Number() { [native code] }可以看到它指向了Number的構(gòu)造函數(shù),因此,可以使用num.constructor==Number來(lái)判斷一個(gè)變量是不是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沒(méi)有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 ); //所有結(jié)果均為true除了undefined和null之外,其他類型都可以通過(guò)constructor屬性來(lái)判斷類型。
使用toString()檢測(cè)對(duì)象類型
可以通過(guò)toString() 來(lái)獲取每個(gè)對(duì)象的類型。為了每個(gè)對(duì)象都能通過(guò) Object.prototype.toString() 來(lái)檢測(cè),需要以 Function.prototype.call() 或者 Function.prototype.apply() 的形式來(lái)調(diào)用,傳遞要檢查的對(duì)象作為第一個(gè)參數(shù),稱為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()的方式來(lái)判斷一個(gè)變量的類型是最準(zhǔn)確的方法。
封裝一個(gè)獲取變量準(zhǔn)確類型的函數(shù)
function gettype(obj) {var type = typeof obj;if (type !== 'object') {return type;}//如果不是object類型的數(shù)據(jù),直接用typeof就能判斷出來(lái)//如果是object類型數(shù)據(jù),準(zhǔn)確判斷類型必須使用Object.prototype.toString.call(obj)的方式才能判斷return Object.prototype.toString.call(obj).replace(/^\[object (\S )\]$/, '$1'); }這樣判斷一個(gè)變量的數(shù)據(jù)類型就很方便了。
更多專業(yè)前端知識(shí),請(qǐng)上 【猿2048】www.mk2048.com
總結(jié)
- 上一篇: 5.jQueryAjax
- 下一篇: 手机端input[type=date]的