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

首頁 > 編程 > JavaScript > 正文

jQuery源碼分析之jQuery中的循環(huán)技巧詳解

2019-11-20 14:09:56
字體:
供稿:網(wǎng)友

jQuery的源碼中有很多值得學(xué)習(xí)借鑒的技巧,本文即收集了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++ ) {  } // 不比較下標(biāo),直接判斷元素是否為true(強制類型轉(zhuǎn)換) var elem; for ( var i = 0; elems[i]; i++ ) {   elem = elems[i];   // ... } // 遍歷動態(tài)數(shù)組(事件),不能緩存length屬性,j++之前先執(zhí)行j--,保證不會因為數(shù)組下標(biāo)的錯誤導(dǎo)致某些數(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 ] );   } } //不判斷下標(biāo),直接判斷元素(選擇器) 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] ); } // 不判斷下標(biāo),取出元素然后判斷元素(選擇器) 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ū)傩?,如parentNode、nextSibling、previousSibling、lastChild和firstChild for ( ; cur; cur = cur[dir] ) {   if ( cur.nodeType === 1 && ++num === result ) {     break;   } } // while檢查下標(biāo)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反復(fù)調(diào)用RegExp.exec(AJAX),能夠否反復(fù)調(diào)是exec比re.test、String.match更加強大的原因,每次調(diào)用都將lastIndex屬性設(shè)置到緊接著匹配字符串的字符位置 while( ( match = rheaders.exec( responseHeadersString ) ) ) {   responseHeaders[ match[1].toLowerCase() ] = match[ 2 ]; // 將響應(yīng)頭以key-value的方式存在responseHeaders中 }

希望本文所述對大家jQuery的WEB程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 扶风县| 大埔县| 密山市| 孟津县| 西盟| 渑池县| 鸡泽县| 夏津县| 馆陶县| 东台市| 红安县| 壶关县| 黄冈市| 克山县| 龙江县| 兰州市| 普格县| 揭东县| 平遥县| 沅陵县| 宝丰县| 黔西县| 响水县| 内黄县| 琼中| 兴仁县| 新平| 金溪县| 卓尼县| 繁峙县| 五河县| 临桂县| 邵武市| 广德县| 乌兰浩特市| 阳江市| 婺源县| 瓮安县| 县级市| 吴桥县| 通道|