jQuery中的類型判斷


在JQuery中有一個type方法,在1.11.2中是這樣寫的

 1 var class2type = {};
 2 var toString = class2type.toString;
 3 jQuery.each("Boolean Number String Function Array Date RegExp Object Error".split(" "), function(i, name) {
 4     class2type[ "[object " + name + "]" ] = name.toLowerCase();
 5 });
 6 type: function( obj ) {
 7     if ( obj == null ) {
 8         return obj + "";
 9     }
10     return typeof obj === "object" || typeof obj === "function" ?
11         class2type[ toString.call(obj) ] || "object" :
12         typeof obj;
13 }

   其核心在於使用Array.prototype.toString.call,因為在任何值上調用Object原生的toString()方法,都會返回一個[object NativeConstructorName]格式的字符串。每個類在內部都有一個[[Class]]屬性,這個屬性中就指定了上述字符串中的構造函數名。

  之所以這樣做是因為js內置的類型檢測機制並非完全的可靠。

  例如,typeof null  輸出“object”

       typeof alert 在IE8中輸出“object”

     typeof [] 輸出“object”

  instanceof如果是跨文檔比較的話,就會存在很大的問題。利用constructor屬性的話,在低版本IE中又會出現問題(window.constructor在IE7下為undefined)。而且,二者不方便判斷基本類型。

  所以說,判斷是否是函數,數組就可以這樣寫

1 isFunction: function( obj ) {
2     return jQuery.type(obj) === "function";
3 },
4 
5 isArray: Array.isArray || function( obj ) {
6     return jQuery.type(obj) === "array";
7 },

  奇怪的是判斷是否為window

1 isWindow: function( obj ) {
2     return obj != null && obj == obj.window;
3 },

  似乎偽裝一下也能騙過去,可能是因為window對象屬於BOM,前面的方法對它不管用。

  然后是isPlainObject,2.0.1中這樣寫。

 1 isPlainObject: function( obj ) {
 2         // Not plain objects:
 3         // - Any object or value whose internal [[Class]] property is not "[object Object]"
 4         // - DOM nodes
 5         // - window
 6         if ( jQuery.type( obj ) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
 7             return false;
 8         }
 9 
10         // Support: Firefox <20
11         // The try/catch suppresses exceptions thrown when attempting to access
12         // the "constructor" property of certain host objects, ie. |window.location|
13         // https://bugzilla.mozilla.org/show_bug.cgi?id=814622
14         try {
15             if ( obj.constructor &&
16                     !core_hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) ) {
17                 return false;
18             }
19         } catch ( e ) {
20             return false;
21         }
22 
23         // If the function hasn't returned already, we're confident that
24         // |obj| is a plain object, created by {} or constructed with new Object
25         return true;
26     }

  這里的PlainObject應該是如{},new Object,這樣的對象。

  第一個if可以排除非Object對象,dom節點,window。

  第二個if是可以排除window.location這樣的BOM對象。

  其中,

  class2type = {},

  core_hasOwn = class2type.hasOwnProperty

  因為這些BOM對象的構造函數的原型肯定不是Object {},自身是沒有isPrototypeOf這個屬性的。

  然后是判斷空對象,也就是沒有可枚舉屬性的對象。

1 isEmptyObject: function( obj ) {
2     var name;
3     for ( name in obj ) {
4         return false;
5     }
6     return true;
7 }

  還有一個函數是用來判斷是否是數字,

//1.11.2
isNumeric: function( obj ) {
    return !jQuery.isArray( obj ) && (obj - parseFloat( obj ) + 1) >= 0;
},

  向parseFloat中傳個數組,如[2,4],返回2,但是數組參加右邊的運算結果肯定是false,不知道為什么要先判斷是否是數組。

  這里如果傳入一個如'123'、'321'這樣的字符串的話也是可以通過的。

  在1.8.0和2.0.1版本中直接就是!isNaN( parseFloat(obj) ) && isFinite( obj ).

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM