通過jQuery學習js類型判斷的技巧
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ū)分數(shù)據(jù)類型,下面是MDN中總結的typeof中所有存在的值

問題一:我們都知道typeof null 出來的結果是‘object',可這是為啥呢?MDN給出了答案 :因為null是空指針,而空指針在大多數(shù)平臺中使用0x00表示,而js在實現(xiàn)初期通過用 0 作為對象的標簽,所以對null也被判斷為object。
問題二:既然typeof能夠判斷出function,為何jquery額外判斷 typeof obj.nodeType !== "number" 呢?
long long ago,在那些古老的瀏覽器中:
1. typeof document.body.childNodes // function 這在古老的 safari 3 中會出現(xiàn)
2.typeof document.createElement("object") // function 同理還有 'embed' 'applet' , 在古老的firefox中會出現(xiàn),目前新版本不會存在
3.typeof /s/ // function 這種情況會在古老瀏覽器中出現(xiàn),目前都會被判定為 object
通過以上問題我們可以看出,通過typeof判斷數(shù)據(jù)類型在古老的瀏覽器中是極為不靠譜的,所以在jquery的isFunction的判斷中額外添加了判斷 檢測對象是否為dom 對象2.靠譜的數(shù)據(jù)類型判斷
源碼:
var class2type = {};
var toString = class2type.toString;
// Populate the class2type map,這里并沒有undefined
jQuery.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 數(shù)據(jù)結構如下:

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 // 對于判斷不出的數(shù)據(jù)類型默認為object
4 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 這些數(shù)據(jù)類型,但是并不能判斷出null,所以要綜合判斷,就醬
除此之外jquery還額外判斷了當前對象是否為window,只用了如下的方法:
var isWindow = function isWindow( obj ) {
return obj != null && obj === obj.window;
};
前方的obj!=null 是為了防止開發(fā)人員在調(diào)用函數(shù) isWindow時傳入null 、undefined的時候報Uncaught TypeError: Cannot read property 'window' of null/undefined的錯誤。
還有isArrayLike,判斷當前對象是不是類數(shù)組對象,類數(shù)組對象是什么,建議大家百度一下
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 IE
var 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ù)組對象集合當然包括數(shù)組) 2.存在length屬性但length是0 3.判定length是數(shù)字且大于零,并在obj對象中存在length-1屬性
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
基于JS實現(xiàn)數(shù)字+字母+中文的混合排序方法
這篇文章主要介紹了基于JS實現(xiàn)數(shù)字+字母+中文的混合排序方法的相關資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-06-06
CodeMirror2 IE7/IE8 下面未知運行時錯誤的解決方法
最近用CodeMirror2作后臺的模板編輯器,在IE9、Firefox下面沒有問題。到了IE7、IE8下面,textarea里面的代碼就顯示不出來了。搜索了好多,終于找到原因2012-03-03
JS如何使用正則表達式(match)截取括號中的文字和數(shù)字
正則表達式是一種用來匹配文本模式的工具,這篇文章主要給大家介紹了關于JS如何使用正則表達式(match)截取括號中文字和數(shù)字的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-10-10
JavaScript 如何計算文本的行數(shù)的實現(xiàn)
這篇文章主要介紹了JavaScript 如何計算文本的行數(shù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09

