js 判斷數(shù)據(jù)類型的幾種方法
判斷js中的數(shù)據(jù)類型有一下幾種方法:typeof、instanceof、 constructor、 prototype、 $.type()/jquery.type(),接下來主要比較一下這幾種方法的異同。
先舉幾個例子:
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類型的對象時比較方便?!?br />
2、判斷已知對象類型的方法: instanceof
alert(c instanceof Array) ---------------> true alert(d instanceof Date) alert(f instanceof Function) ------------> true alert(f instanceof function) ------------> false
注意:instanceof 后面一定要是對象類型,并且大小寫不能錯,該方法適合一些條件選擇或分支。
3、根據(jù)對象的constructor判斷: constructor
alert(c.constructor === Array) ----------> true
alert(d.constructor === Date) -----------> true
alert(e.constructor === Function) -------> true
注意: constructor 在類繼承時會出錯
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方法不會出現(xiàn)該問題,對象直接繼承和間接繼承的都會報true:
alert(aobj instanceof B) ----------------> true; alert(aobj instanceof B) ----------------> true;
言歸正傳,解決construtor的問題通常是讓對象的constructor手動指向自己:
aobj.constructor = A; //將自己的類賦值給對象的constructor屬性 alert(aobj.constructor === A) -----------> true; alert(aobj.constructor === B) -----------> false; //基類不會報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;
大小寫不能寫錯,比較麻煩,但勝在通用。
5、無敵萬能的方法:jquery.type()
如果對象是undefined或null,則返回相應(yīng)的“undefined”或“null”。
jQuery.type( undefined ) === "undefined" jQuery.type() === "undefined" jQuery.type( window.notDefined ) === "undefined" jQuery.type( null ) === "null"
如果對象有一個內(nèi)部的[[Class]]和一個瀏覽器的內(nèi)置對象的 [[Class]] 相同,我們返回相應(yīng)的 [[Class]] 名字。 (有關(guān)此技術(shù)的更多細節(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方法,實在沒轍就使用$.type()方法。
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
類似php的js數(shù)組的in_array函數(shù)自定義方法
PHP的數(shù)組函數(shù)in_array()非常方便,下面就為大家介紹下自定義類似php的js數(shù)組的in_array函數(shù),具體實現(xiàn)方法如下,感興趣的朋友可以參考下2013-12-12
JavaScript獲取網(wǎng)頁表單action屬性的方法
這篇文章主要介紹了JavaScript獲取網(wǎng)頁表單action屬性的方法,涉及javascript操作表單屬性的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04
Typescript中Type check類型檢查的實現(xiàn)
本文主要介紹了Typescript中Type check類型檢查的實現(xiàn),類型檢查是一項非常重要的特性,它可以幫助你捕獲潛在的錯誤,下面就來詳細的介紹一下如何實現(xiàn),感興趣的可以了解一下2025-10-10
layui的數(shù)據(jù)表格+springmvc實現(xiàn)搜索功能的例子
今天小編就為大家分享一篇layui的數(shù)據(jù)表格+springmvc實現(xiàn)搜索功能的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
TypeScript中定義變量方式以及數(shù)據(jù)類型詳解
TypeScript支持 JavaScript的所有語法和語義,同時通過作為ECMAScript的超集來提供一些額外的功能,如類型檢測和更豐富的語法,這篇文章主要給大家介紹了關(guān)于TypeScript中定義變量方式以及數(shù)據(jù)類型詳解的相關(guān)資料,需要的朋友可以參考下2022-08-08
JS獲取select-option-text_value的方法
這篇文章主要介紹了JS獲取select-option-text_value的方法,有需要的朋友可以參考一下2013-12-12

