国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > JS > 正文

js 判斷數據類型的幾種方法

2024-05-06 16:34:33
字體:
來源:轉載
供稿:網友

判斷js中的數據類型有一下幾種方法: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)  ------------> stringalert(typeof b)  ------------> numberalert(typeof c)  ------------> objectalert(typeof d)  ------------> objectalert(typeof e)  ------------> functionalert(typeof f)  ------------> function

其中typeof返回的類型都是字符串形式,需注意,例如:

alert(typeof a == "string") -------------> truealert(typeof a == String) ---------------> false

另外typeof 可以判斷function的類型;在判斷除Object類型的對象時比較方便。 

2、判斷已知對象類型的方法: instanceof

alert(c instanceof Array) ---------------> truealert(d instanceof Date)alert(f instanceof Function) ------------> truealert(f instanceof function) ------------> false

注意:instanceof 后面一定要是對象類型,并且大小寫不能錯,該方法適合一些條件選擇或分支。   

3、根據對象的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方法不會出現該問題,對象直接繼承和間接繼承的都會報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,則返回相應的“undefined”或“null”。

jQuery.type( undefined ) === "undefined"jQuery.type() === "undefined"jQuery.type( window.notDefined ) === "undefined"jQuery.type( null ) === "null"

如果對象有一個內部的[[Class]]和一個瀏覽器的內置對象的 [[Class]] 相同,我們返回相應的 [[Class]] 名字。 (有關此技術的更多細節。 )

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.9jQuery.type( /test/ ) === "regexp"

其他一切都將返回它的類型“object”。

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

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持VeVb武林網!


注:相關教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 习水县| 榆树市| 板桥市| 比如县| 灯塔市| 漳州市| 中宁县| 夏津县| 南通市| 东城区| 邹城市| 抚松县| 随州市| 刚察县| 鄂州市| 和硕县| 呼伦贝尔市| 泰州市| 诸城市| 丘北县| 新龙县| 祥云县| 平塘县| 筠连县| 南溪县| 浦东新区| 岱山县| 肃宁县| 丹棱县| 乐清市| 普定县| 大厂| 耒阳市| 通化县| 云浮市| 遵义市| 荥阳市| 江陵县| 开化县| 拜城县| 中山市|