判斷js中的資料類型的幾種方法

來源:互聯網
上載者:User

標籤:fine   通用   pre   ror   not   null   物件類型   color   mst   

判斷js中的資料類型有一下幾種方法:typeof、instanceof、 constructor、 prototype、 $.type()/jquery.type(),接下來主要比較一下這幾種方法的異同。

先舉幾個例子:

1 var a = "iamstring.";2 var b = 222;3 var c= [1,2,3];4 var d = new Date();5 var e = function(){alert(111);};6 var f = function(){this.name="22";};
1、最常見的判斷方法:typeof
 1 alert(typeof a)   ------------> string 2 alert(typeof b)   ------------> number 3 alert(typeof c)   ------------> object 4 alert(typeof d)   ------------> object 5 alert(typeof e)   ------------> function 6 alert(typeof f)   ------------> function 7 其中typeof返回的類型都是字串形式,需注意,例如: 8 alert(typeof a == "string") -------------> true 9 alert(typeof a == String) ---------------> false10 另外typeof 可以判斷function的類型;在判斷除Object類型的對象時比較方便。
2、判斷已知物件類型的方法: instanceof
1 alert(c instanceof Array) ---------------> true2 alert(d instanceof Date) 3 alert(f instanceof Function) ------------> true4 alert(f instanceof function) ------------> false5 注意:instanceof 後面一定要是物件類型,並且大小寫不能錯,該方法適合一些條件選擇或分支。
3、根據對象的constructor判斷: constructor
 1 alert(c.constructor === Array) ----------> true 2 alert(d.constructor === Date) -----------> true 3 alert(e.constructor === Function) -------> true 4 注意: constructor 在類繼承時會出錯 5 eg: 6       function A(){}; 7       function B(){}; 8       A.prototype = new B(); //A繼承自B 9       var aObj = new A();10       alert(aobj.constructor === B) -----------> true;11       alert(aobj.constructor === A) -----------> false;12 而instanceof方法不會出現該問題,對象直接繼承和間接繼承的都會報true:13       alert(aobj instanceof B) ----------------> true;14       alert(aobj instanceof B) ----------------> true;15 言歸正傳,解決construtor的問題通常是讓對象的constructor手動指向自己:16       aobj.constructor = A; //將自己的類賦值給對象的constructor屬性17       alert(aobj.constructor === A) -----------> true;18       alert(aobj.constructor === B) -----------> false; //基類不會報true了;
4、通用但很繁瑣的方法: prototype
1 alert(Object.prototype.toString.call(a) === ‘[object String]’) -------> true;2 alert(Object.prototype.toString.call(b) === ‘[object Number]’) -------> true;3 alert(Object.prototype.toString.call(c) === ‘[object Array]’) -------> true;4 alert(Object.prototype.toString.call(d) === ‘[object Date]’) -------> true;5 alert(Object.prototype.toString.call(e) === ‘[object Function]’) -------> true;6 alert(Object.prototype.toString.call(f) === ‘[object Function]’) -------> true;7 大小寫不能寫錯,比較麻煩,但勝在通用。
5、無敵萬能的方法:jquery.type()
 1 如果對象是undefined或null,則返回相應的“undefined”或“null”。 2 jQuery.type( undefined ) === "undefined" 3 jQuery.type() === "undefined" 4 jQuery.type( window.notDefined ) === "undefined" 5 jQuery.type( null ) === "null" 6 如果對象有一個內部的[[Class]]和一個瀏覽器的內建對象的 [[Class]] 相同,我們返回相應的 [[Class]] 名字。 (有關此技術的更多細節。 ) 7 jQuery.type( true ) === "boolean" 8 jQuery.type( 3 ) === "number" 9 jQuery.type( "test" ) === "string"10 jQuery.type( function(){} ) === "function"11 jQuery.type( [] ) === "array"12 jQuery.type( new Date() ) === "date"13 jQuery.type( new Error() ) === "error" // as of jQuery 1.914 jQuery.type( /test/ ) === "regexp"15 其他一切都將返回它的類型“object”。

通常情況下用typeof 判斷就可以了,遇到預知Object類型的情況可以選用instanceof或constructor方法,實在沒轍就使用$.type()方法。

參考連結:http://blog.sina.com.cn/s/blog_51048da70101grz6.html

判斷js中的資料類型的幾種方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.