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

首頁 > 編程 > JavaScript > 正文

通過jQuery學習js類型判斷的技巧

2019-11-19 11:27:59
字體:
來源:轉載
供稿:網友

1. isFunction中typeof的不靠譜

源碼:

var isFunction = function isFunction( obj ) {// Support: Chrome <=57, Firefox <=52// In some browsers, typeof returns "function" for HTML <object> elements// (i.e., `typeof document.createElement( "object" ) === "function"`).// We don't want to classify *any* DOM node as a function.return typeof obj === "function" && typeof obj.nodeType !== "number";};

typeof 是為了區分數據類型,下面是MDN中總結的typeof中所有存在的值

問題一:我們都知道typeof null 出來的結果是‘object',可這是為啥呢?MDN給出了答案 :因為null是空指針,而空指針在大多數平臺中使用0x00表示,而js在實現初期通過用 0 作為對象的標簽,所以對null也被判斷為object。

問題二:既然typeof能夠判斷出function,為何jquery額外判斷 typeof obj.nodeType !== "number" 呢?

long long ago,在那些古老的瀏覽器中:

1. typeof document.body.childNodes // function 這在古老的 safari 3 中會出現

2.typeof document.createElement("object") // function 同理還有 'embed' 'applet' , 在古老的firefox中會出現,目前新版本不會存在

3.typeof /s/ // function 這種情況會在古老瀏覽器中出現,目前都會被判定為 object

通過以上問題我們可以看出,通過typeof判斷數據類型在古老的瀏覽器中是極為不靠譜的,所以在jquery的isFunction的判斷中額外添加了判斷 檢測對象是否為dom 對象2.靠譜的數據類型判斷

源碼:

var class2type = {};var toString = class2type.toString;// Populate the class2type map,這里并沒有undefinedjQuery.each( "Boolean Number String Function Array Date RegExp Object Error Symbol".split( " " ),function( i, name ) {class2type[ "[object " + name + "]" ] = name.toLowerCase();} );function toType( obj ) {if ( obj == null ) {return obj + "";}// Support: Android <=2.3 only (functionish RegExp)return typeof obj === "object" || typeof obj === "function" ?class2type[ toString.call( obj ) ] || "object" :typeof obj;}

在代碼中jquery做了這幾件事:

1.jquery先提取出toString 這個方法

2.將寫好的類型字符串分割并存入class2type中,class2type 數據結構如下:

3.定義toType方法,因為 toString(null)會得出‘ [object Undefined]'的結果,所以需要把null單獨判斷,注意null是沒有toString這個方法的,所以通過 obj+''這個方式得到 'null'

4.在單獨判斷null后是一個三元運算符:等價于

1 if(typeof obj === "object" || typeof obj === "function"){2 // 因為上文提到存在typeof /s/ 為 function的情況,所以需要toString詳細判斷3 // 對于判斷不出的數據類型默認為object4 retrun class2type[ toString.call( obj ) ] || "object";5 } else {6 // 通過上面typeof對類型判斷的表格,判斷非object function還是很可靠的,所以直接用原生方法7 return typeof obj;8 }

結論: 通過用toString方法可以判斷出Boolean、Number、 String、 Function、 Array、 Date、 RegExp、 Object、 Error、 Symbol、undefined 這些數據類型,但是并不能判斷出null,所以要綜合判斷,就醬

除此之外jquery還額外判斷了當前對象是否為window,只用了如下的方法:

var isWindow = function isWindow( obj ) {return obj != null && obj === obj.window;};

前方的obj!=null 是為了防止開發人員在調用函數 isWindow時傳入null 、undefined的時候報Uncaught TypeError: Cannot read property 'window' of null/undefined的錯誤。

還有isArrayLike,判斷當前對象是不是類數組對象,類數組對象是什么,建議大家百度一下

function isArrayLike( obj ) {// Support: real iOS 8.2 only (not reproducible in simulator)// `in` check used to prevent JIT error (gh-2145)// hasOwn isn't used here due to false negatives// regarding Nodelist length in IEvar length = !!obj && "length" in obj && obj.length,type = toType( obj );if ( isFunction( obj ) || isWindow( obj ) ) {return false;}return type === "array" || length === 0 ||typeof length === "number" && length > 0 && ( length - 1 ) in obj;}

首先判斷obj中是否有length屬性并取出length

然后排除obj是否是window 及 function

最后取值條件:1.是否是array(類數組對象集合當然包括數組) 2.存在length屬性但length是0 3.判定length是數字且大于零,并在obj對象中存在length-1屬性

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 奉新县| 嘉祥县| 瑞金市| 醴陵市| 长泰县| 广西| 遂川县| 南安市| 大田县| 固镇县| 云林县| 钟祥市| 阳城县| 宣汉县| 四子王旗| 丰宁| 博爱县| 达日县| 个旧市| 平谷区| 舞钢市| 环江| 资兴市| 囊谦县| 华池县| 甘孜县| 华蓥市| 武鸣县| 颍上县| 敖汉旗| 博客| 扶绥县| 呼和浩特市| 康马县| 尉氏县| 贵州省| 萝北县| 商都县| 台州市| 汪清县| 柳林县|