js 判斷數(shù)據(jù)類(lèi)型的幾種方法
判斷js中的數(shù)據(jù)類(lèi)型有一下幾種方法:typeof、instanceof、 constructor、 prototype、 $.type()/jquery.type(),接下來(lái)主要比較一下這幾種方法的異同。
先舉幾個(gè)例子:
var a = "iamstring.";
var b = 222;
var c= [1,2,3];
var d = new Date();
var e = function(){alert(111);};
var f = function(){this.name="22";};
1、最常見(jiàn)的判斷方法:typeof
alert(typeof a) ------------> string alert(typeof b) ------------> number alert(typeof c) ------------> object alert(typeof d) ------------> object alert(typeof e) ------------> function alert(typeof f) ------------> function
其中typeof返回的類(lèi)型都是字符串形式,需注意,例如:
alert(typeof a == "string") -------------> true alert(typeof a == String) ---------------> false
另外typeof 可以判斷function的類(lèi)型;在判斷除Object類(lèi)型的對(duì)象時(shí)比較方便?!?br />
2、判斷已知對(duì)象類(lèi)型的方法: instanceof
alert(c instanceof Array) ---------------> true alert(d instanceof Date) alert(f instanceof Function) ------------> true alert(f instanceof function) ------------> false
注意:instanceof 后面一定要是對(duì)象類(lèi)型,并且大小寫(xiě)不能錯(cuò),該方法適合一些條件選擇或分支?!?/span>
3、根據(jù)對(duì)象的constructor判斷: constructor
alert(c.constructor === Array) ----------> true
alert(d.constructor === Date) -----------> true
alert(e.constructor === Function) -------> true
注意: constructor 在類(lèi)繼承時(shí)會(huì)出錯(cuò)
eg:
function A(){};
function B(){};
A.prototype = new B(); //A繼承自B
var aObj = new A();
alert(aobj.constructor === B) -----------> true;
alert(aobj.constructor === A) -----------> false;
而instanceof方法不會(huì)出現(xiàn)該問(wèn)題,對(duì)象直接繼承和間接繼承的都會(huì)報(bào)true:
alert(aobj instanceof B) ----------------> true; alert(aobj instanceof B) ----------------> true;
言歸正傳,解決construtor的問(wèn)題通常是讓對(duì)象的constructor手動(dòng)指向自己:
aobj.constructor = A; //將自己的類(lèi)賦值給對(duì)象的constructor屬性 alert(aobj.constructor === A) -----------> true; alert(aobj.constructor === B) -----------> false; //基類(lèi)不會(huì)報(bào)true了;
4、通用但很繁瑣的方法: prototype
alert(Object.prototype.toString.call(a) === ‘[object String]') -------> true; alert(Object.prototype.toString.call(b) === ‘[object Number]') -------> true; alert(Object.prototype.toString.call(c) === ‘[object Array]') -------> true; alert(Object.prototype.toString.call(d) === ‘[object Date]') -------> true; alert(Object.prototype.toString.call(e) === ‘[object Function]') -------> true; alert(Object.prototype.toString.call(f) === ‘[object Function]') -------> true;
大小寫(xiě)不能寫(xiě)錯(cuò),比較麻煩,但勝在通用。
5、無(wú)敵萬(wàn)能的方法:jquery.type()
如果對(duì)象是undefined或null,則返回相應(yīng)的“undefined”或“null”。
jQuery.type( undefined ) === "undefined" jQuery.type() === "undefined" jQuery.type( window.notDefined ) === "undefined" jQuery.type( null ) === "null"
如果對(duì)象有一個(gè)內(nèi)部的[[Class]]和一個(gè)瀏覽器的內(nèi)置對(duì)象的 [[Class]] 相同,我們返回相應(yīng)的 [[Class]] 名字。 (有關(guān)此技術(shù)的更多細(xì)節(jié)。 )
jQuery.type( true ) === "boolean"
jQuery.type( 3 ) === "number"
jQuery.type( "test" ) === "string"
jQuery.type( function(){} ) === "function"
jQuery.type( [] ) === "array"
jQuery.type( new Date() ) === "date"
jQuery.type( new Error() ) === "error" // as of jQuery 1.9
jQuery.type( /test/ ) === "regexp"
其他一切都將返回它的類(lèi)型“object”。
通常情況下用typeof 判斷就可以了,遇到預(yù)知Object類(lèi)型的情況可以選用instanceof或constructor方法,實(shí)在沒(méi)轍就使用$.type()方法。
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
Javascript實(shí)現(xiàn)的CSS代碼高亮顯示
到網(wǎng)上一位別出心裁的高手使用字符串而不是正則表達(dá)式實(shí)現(xiàn)了Javascript代碼高亮顯示,自己受益匪淺,于是就構(gòu)思了CSS代碼的高亮顯示。2009-11-11
textarea支持圖形編輯的實(shí)現(xiàn)方法
本來(lái)以為只有iframe支持編輯了,今天突然發(fā)現(xiàn)textarea也支持編輯 :( 是不是我太愚鈍了? textarea不能用innerHTML來(lái)插入html,而用appendChild 這樣的話(huà)稍微修改下,評(píng)論等簡(jiǎn)單的textarea的表情插入就可以直接顯示出來(lái)了 而不是只顯示emot了 比較實(shí)用標(biāo)題起的確切,但如果叫“現(xiàn)textarea支持編輯”似乎更廢話(huà)2008-03-03
使用mpvue搭建一個(gè)初始小程序及項(xiàng)目配置方法
這篇文章主要介紹了使用mpvue搭建一個(gè)初始小程序及項(xiàng)目配置方法,需要的朋友可以參考下2018-12-12
JS實(shí)現(xiàn)獲取來(lái)自百度,Google,soso,sogou關(guān)鍵詞的方法
這篇文章主要介紹了JS實(shí)現(xiàn)獲取來(lái)自百度,Google,soso,sogou關(guān)鍵詞的方法,結(jié)合實(shí)例形式分析了js獲取來(lái)路頁(yè)面的方法與相關(guān)搜索引擎關(guān)鍵詞的處理技巧,需要的朋友可以參考下2016-12-12
詳解JavaScript什么情況下不建議使用箭頭函數(shù)
箭頭函數(shù)作為ES6新增的語(yǔ)法,在使用時(shí)不僅能使得代碼更加簡(jiǎn)潔,而且在某些場(chǎng)景避免this指向問(wèn)題。但是箭頭函數(shù)不是萬(wàn)能的,也有自己的缺點(diǎn)以及不適用的場(chǎng)景,本文總結(jié)了JavaScript什么情況下不建議使用箭頭函數(shù),感興趣的可以了解一下2022-06-06

