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 是為了區(qū)分?jǐn)?shù)據(jù)類型,下面是MDN中總結(jié)的typeof中所有存在的值
問(wèn)題一:我們都知道typeof null 出來(lái)的結(jié)果是‘object',可這是為啥呢?MDN給出了答案 :因?yàn)閚ull是空指針,而空指針在大多數(shù)平臺(tái)中使用0x00表示,而js在實(shí)現(xiàn)初期通過(guò)用 0 作為對(duì)象的標(biāo)簽,所以對(duì)null也被判斷為object。
問(wèn)題二:既然typeof能夠判斷出function,為何jquery額外判斷 typeof obj.nodeType !== "number" 呢?
long long ago,在那些古老的瀏覽器中:
1. typeof document.body.childNodes // function 這在古老的 safari 3 中會(huì)出現(xiàn)
2.typeof document.createElement("object") // function 同理還有 'embed' 'applet' , 在古老的firefox中會(huì)出現(xiàn),目前新版本不會(huì)存在
3.typeof /s/ // function 這種情況會(huì)在古老瀏覽器中出現(xiàn),目前都會(huì)被判定為 object
通過(guò)以上問(wèn)題我們可以看出,通過(guò)typeof判斷數(shù)據(jù)類型在古老的瀏覽器中是極為不靠譜的,所以在jquery的isFunction的判斷中額外添加了判斷 檢測(cè)對(duì)象是否為dom 對(duì)象
2.靠譜的數(shù)據(jù)類型判斷
源碼:
var class2type = {};var toString = class2type.toString;// Populate the class2type map,這里并沒(méi)有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 這個(gè)方法
2.將寫好的類型字符串分割并存入class2type中,class2type 數(shù)據(jù)結(jié)構(gòu)如下:
3.定義toType方法,因?yàn)?toString(null)會(huì)得出‘ [object Undefined]'的結(jié)果,所以需要把null單獨(dú)判斷,注意null是沒(méi)有toString這個(gè)方法的,所以通過(guò) obj+''這個(gè)方式得到 'null'
4.在單獨(dú)判斷null后是一個(gè)三元運(yùn)算符:等價(jià)于
1 if(typeof obj === "object" || typeof obj === "function"){2 // 因?yàn)樯衔奶岬酱嬖趖ypeof /s/ 為 function的情況,所以需要toString詳細(xì)判斷3 // 對(duì)于判斷不出的數(shù)據(jù)類型默認(rèn)為object4 retrun class2type[ toString.call( obj ) ] || "object";5 } else {6 // 通過(guò)上面typeof對(duì)類型判斷的表格,判斷非object function還是很可靠的,所以直接用原生方法7 return typeof obj;8 }結(jié)論: 通過(guò)用toString方法可以判斷出Boolean、Number、 String、 Function、 Array、 Date、 RegExp、 Object、 Error、 Symbol、undefined 這些數(shù)據(jù)類型,但是并不能判斷出null,所以要綜合判斷,就醬
除此之外jquery還額外判斷了當(dāng)前對(duì)象是否為window,只用了如下的方法:
var isWindow = function isWindow( obj ) {return obj != null && obj === obj.window;};前方的obj!=null 是為了防止開(kāi)發(fā)人員在調(diào)用函數(shù) isWindow時(shí)傳入null 、undefined的時(shí)候報(bào)Uncaught TypeError: Cannot read property 'window' of null/undefined的錯(cuò)誤。
還有isArrayLike,判斷當(dāng)前對(duì)象是不是類數(shù)組對(duì)象,類數(shù)組對(duì)象是什么,建議大家百度一下
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中是否有l(wèi)ength屬性并取出length
然后排除obj是否是window 及 function
最后取值條件:1.是否是array(類數(shù)組對(duì)象集合當(dāng)然包括數(shù)組) 2.存在length屬性但length是0 3.判定length是數(shù)字且大于零,并在obj對(duì)象中存在length-1屬性
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。
新聞熱點(diǎn)
疑難解答