jQuery源碼分析之jQuery中的循環(huán)技巧詳解
更新時間:2014年09月06日 16:02:27 投稿:shichen2014
這篇文章主要介紹了jQuery源碼中的循環(huán)技巧,包括各類選擇、判斷、遍歷等等操作,非常實用的技巧,需要的朋友可以參考下
jQuery的源碼中有很多值得學習借鑒的技巧,本文即收集了jQuery中出現(xiàn)的各種遍歷技巧和場景。具體分析如下:
// 簡單的for-in(事件) for ( type in events ) { } // 緩存length屬性,避免每次都去查找length屬性,稍微提升遍歷速度 // 但是如果遍歷HTMLCollection時,性能提升非常明顯,因為每次訪問HTMLCollection的屬性,HTMLCollection都會內(nèi)部匹配一次所有的節(jié)點 for ( var j = 0, l = handlers.length; j < l; j++ ) { } // 不比較下標,直接判斷元素是否為true(強制類型轉(zhuǎn)換) var elem; for ( var i = 0; elems[i]; i++ ) { elem = elems[i]; // ... } // 遍歷動態(tài)數(shù)組(事件),不能緩存length屬性,j++之前先執(zhí)行j--,保證不會因為數(shù)組下標的錯誤導致某些數(shù)組元素遍歷不到 for ( j = 0; j < eventType.length; j++ ) { eventType.splice( j--, 1 ); } for ( var i = 1; i < results.length; i++ ) { if ( results[i] === results[ i - 1 ] ) { results.splice( i--, 1 ); } } // 迭代過程中盡可能減少遍歷次數(shù)(事件),如果你能知道從哪里開始遍歷的話,這里是pos for ( j = pos || 0; j < eventType.length; j++ ) { } //倒序遍歷(事件),減少了幾個字符:循環(huán)條件判斷,合并i自減和i取值,倒序遍歷會有瀏覽器優(yōu)化,稍微提升遍歷速度 for ( var i = this.props.length, prop; i; ) { prop = this.props[ --i ]; event[ prop ] = originalEvent[ prop ]; } // 倒序遍歷,中規(guī)中矩,倒序會有瀏覽器優(yōu)化,稍微提升遍歷速度 for ( j = tbody.length - 1; j >= 0 ; --j ) { if ( jQuery.nodeName( tbody[ j ], "tbody" ) && !tbody[ j ].childNodes.length ) { tbody[ j ].parentNode.removeChild( tbody[ j ] ); } } //不判斷下標,直接判斷元素(選擇器) for ( i = 0; checkSet[i] != null; i++ ) { if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && Sizzle.contains(context, checkSet[i])) ) { results.push( set[i] ); } } for ( ; array[i]; i++ ) { ret.push( array[i] ); } // 不判斷下標,取出元素然后判斷元素(選擇器) for ( var i = 0; (item = curLoop[i]) != null; i++ ) { } // 遍歷DOM子元素 for ( node = parent.firstChild; node; node = node.nextSibling ) { if ( node.nodeType === 1 ) { node.nodeIndex = ++count; } } // 動態(tài)遍歷DOM子元素(DOM遍歷),dir參數(shù)表示元素的方向?qū)傩裕鏿arentNode、nextSibling、previousSibling、lastChild和firstChild for ( ; cur; cur = cur[dir] ) { if ( cur.nodeType === 1 && ++num === result ) { break; } } // while檢查下標i var i = promiseMethods.length; while( i-- ) { obj[ promiseMethods[i] ] = deferred[ promiseMethods[i] ]; } // while檢查元素 while( (type = types[ i++ ]) ) { } // while遍歷動態(tài)數(shù)組(AJAX),總是獲取第一個元素,檢查是否與特殊值相等,如果相等就從數(shù)組頭部移除,直到遇到不相等的元素或數(shù)組為空 while( dataTypes[ 0 ] === "*" ) { dataTypes.shift(); if ( ct === undefined ) { ct = s.mimeType || jqXHR.getResponseHeader( "content-type" ); } } // while遍歷動態(tài)數(shù)組(異步隊列),總是獲取第一個元素,直到數(shù)組為空,或遇到值為undefined的元素 while( callbacks[ 0 ] ) { callbacks.shift().apply( context, args ); } // while反復調(diào)用RegExp.exec(AJAX),能夠否反復調(diào)是exec比re.test、String.match更加強大的原因,每次調(diào)用都將lastIndex屬性設置到緊接著匹配字符串的字符位置 while( ( match = rheaders.exec( responseHeadersString ) ) ) { responseHeaders[ match[1].toLowerCase() ] = match[ 2 ]; // 將響應頭以key-value的方式存在responseHeaders中 }
希望本文所述對大家jQuery的WEB程序設計有所幫助。
相關(guān)文章
jQuery EasyUI學習教程之datagrid點擊列表頭排序
這篇文章主要介紹了jQuery EasyUI學習教程之datagrid點擊列表頭排序的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07javascript loadScript異步加載腳本示例講解
本文講解了javascript異步加載腳本并觸發(fā)回調(diào)函數(shù)的方法,在加載遠程數(shù)據(jù)的時候可以用到,下面提供代碼示例和源碼2013-11-11使用jquery-easyui的布局layout寫后臺管理頁面的代碼詳解
這篇文章主要介紹了使用jquery-easyui的布局layout寫后臺管理頁面的代碼詳解,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06