js 判斷數(shù)據(jù)類型的幾種方法
判斷js中的數(shù)據(jù)類型有一下幾種方法: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、最常見的判斷方法: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返回的類型都是字符串形式,需注意,例如:
alert(typeof a == "string") -------------> true alert(typeof a == String) ---------------> false
另外typeof 可以判斷function的類型;在判斷除Object類型的對(duì)象時(shí)比較方便?!?br />
2、判斷已知對(duì)象類型的方法: instanceof
alert(c instanceof Array) ---------------> true alert(d instanceof Date) alert(f instanceof Function) ------------> true alert(f instanceof function) ------------> false
注意:instanceof 后面一定要是對(duì)象類型,并且大小寫不能錯(cuò),該方法適合一些條件選擇或分支?!?/span>
3、根據(jù)對(duì)象的constructor判斷: constructor
alert(c.constructor === Array) ----------> true
alert(d.constructor === Date) -----------> true
alert(e.constructor === Function) -------> true
注意: constructor 在類繼承時(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)該問題,對(duì)象直接繼承和間接繼承的都會(huì)報(bào)true:
alert(aobj instanceof B) ----------------> true; alert(aobj instanceof B) ----------------> true;
言歸正傳,解決construtor的問題通常是讓對(duì)象的constructor手動(dòng)指向自己:
aobj.constructor = A; //將自己的類賦值給對(duì)象的constructor屬性 alert(aobj.constructor === A) -----------> true; alert(aobj.constructor === B) -----------> false; //基類不會(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;
大小寫不能寫錯(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"
其他一切都將返回它的類型“object”。
通常情況下用typeof 判斷就可以了,遇到預(yù)知Object類型的情況可以選用instanceof或constructor方法,實(shí)在沒轍就使用$.type()方法。
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
JavaScript實(shí)現(xiàn)簡(jiǎn)單的日歷效果
本文給大家分享的是一個(gè)簡(jiǎn)單的JavaScript制作的日歷模板,小伙伴們可以根據(jù)自己的需求,繼續(xù)補(bǔ)充,希望大家能夠喜歡2016-09-09
javascript 多種搜索引擎集成的頁(yè)面實(shí)現(xiàn)代碼
這個(gè)頁(yè)面是為了方便自己同時(shí)使用多種搜索引擎(呵呵我用其作默認(rèn)主頁(yè)),在IE5/IE6/FireFox下均運(yùn)行正常,效果如下圖2010-01-01
javascript實(shí)現(xiàn)單擊和雙擊并存的方法
這篇文章主要介紹了javascript實(shí)現(xiàn)單擊和雙擊并存的方法,可通過定義二次點(diǎn)擊的間隔時(shí)間來(lái)達(dá)到判斷單擊與雙擊的效果,是非常實(shí)用的技巧,需要的朋友可以參考下2014-12-12
前端HTTP發(fā)POST請(qǐng)求攜帶參數(shù)與后端接口接收參數(shù)的實(shí)現(xiàn)
近期在學(xué)習(xí)的時(shí)候,碰到一個(gè)關(guān)于post的小問題,故拿出來(lái)分享一下,下面這篇文章主要給大家介紹了關(guān)于前端HTTP發(fā)POST請(qǐng)求攜帶參數(shù)與后端接口接收參數(shù)的相關(guān)資料,需要的朋友可以參考下2022-10-10
如何解決IONIC頁(yè)面底部被遮住無(wú)法向上滾動(dòng)問題
Ionic 是目前最有潛力的一款 HTML5 手機(jī)應(yīng)用開發(fā)框架。在開發(fā)過程中我們同樣會(huì)遇到各種各樣奇葩的問題。下面小編給大家?guī)?lái)了有關(guān)IONIC頁(yè)面底部被遮住無(wú)法向上滾動(dòng)問題的解決方案2016-09-09
原生JavaScript+LESS實(shí)現(xiàn)瀑布流
這篇文章主要介紹了原生JavaScript+LESS實(shí)現(xiàn)瀑布流的方法,附上了具體實(shí)例,這里推薦給有需要的小伙伴。2014-12-12
JavaScript自定義超時(shí)API代碼實(shí)例
這篇文章主要介紹了JavaScript自定義超時(shí)API代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04

