>  기사  >  웹 프론트엔드  >  jQuery에서 .makeArray()의 코드 읽기

jQuery에서 .makeArray()의 코드 읽기

黄舟
黄舟원래의
2017-07-19 09:41:151116검색

jquery의 makeArray 함수는 배열과 유사한 객체를 배열로 변환할 수 있습니다. 공식 API 설명과 테스트 예제는 여기에 있습니다(Convert 배열 유사 객체를 진정한 JavaScript 배열로 변환합니다.) 그렇다면 배열 유사 객체란 무엇일까요? (배열형 객체) 이것이 Arrary-Like

Array-Like Object

의 정의입니다. 진정한 JavaScript 배열이거나 음수가 아닌 정수 길이 속성 0 최대 길이 - 1 이 후자의 경우에는 인수 객체 및 NodeList 객체는 많은 DOM 메서드에서 반환됩니다.length property and index properties from 0 up to length - 1. This latter case includes array-like objects commonly encountered in web-based code such as the arguments object and the NodeList object returned by many DOM methods.

When a jQuery API accepts either plain Objects or Array-Like objects, a plain Object with a numeric length

jQuery API가 일반 객체 또는 유사 배열 객체를 허용하는 경우 숫자 길이 >속성 유사 배열 동작을 트리거합니다.<p style="margin-top:0px; margin-bottom:15px; color:rgb(51,51,51); font-family:'Helvetica Neue',HelveticaNeue,Helvetica,Arial,sans-serif; font-size:15px; line-height:22.5px"></p> 길이 속성을 포함하며 값은 음수가 아니며 아래 첨자에 따라 속성에 액세스할 수 있습니다. 일반적인 배열 유사 객체에는 aruments, NodeList<p style="margin-top:0px; margin-bottom:15px; color:rgb(51,51,51); font-family:'Helvetica Neue',HelveticaNeue,Helvetica,Arial,sans-serif; font-size:15px; line-height:22.5px"><br></p> <p><pre class="brush:js;toolbar:false;">// results is for internal usage only result是jquery内部使用的参数,如果不为空则把array并到resuls上 makeArray: function( array, results ) { var ret = results || []; if ( array != null ) { // The window, strings (and functions) also have &amp;#39;length&amp;#39; // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930 var type = jQuery.type( array ); //array没有length属性,或者为string类型,function类型,window类型,或者黑莓中正则对象,黑莓中正则对象也含有length对象,则push到result if ( array.length == null || type === &quot;string&quot; || type === &quot;function&quot; || type === &quot;regexp&quot; || jQuery.isWindow( array ) ) { push.call( ret, array ); } else { //调用merge把类数组array合并到ret jQuery.merge( ret, array ); } } return ret; }</pre><br>여기에서 호출되는 jquery.type</p> <p style="margin-top:0px; margin-bottom:15px; color:rgb(51,51,51); font-family:'Helvetica Neue',HelveticaNeue,Helvetica,Arial,sans-serif; font-size:15px; line-height:22.5px"></p>jquery.isWindow() 1.7.1이 포함됩니다. obj에 setInterval 속성 "setInterval"이 포함되어 있는지 여부에 따라 판단됩니다. 앞으로는 window 속성에 따라 레이아웃이 판단됩니다. obj == obj.window<p style="margin-top:0px; margin-bottom:15px; color:rgb(51,51,51); font-family:'Helvetica Neue',HelveticaNeue,Helvetica,Arial,sans-serif; font-size:15px; line-height:22.5px"><br></p> <p><pre class="brush:js;toolbar:false;">isWindow:function(obj){ return obj &amp;&amp; typeof obj === &quot;object&quot; &amp;&amp; &quot;setInterval&quot; in obj; //1.7.2: return obj != null &amp;&amp; obj == obj.window; },</pre><br></p> jQuery.merge(frist, second)의 공식 설명은 여기서 호출됩니다. 두 번째 배열 또는 배열과 유사한 개체가 첫 번째 배열로 병합됩니다. <p style="margin-top:0px; margin-bottom:15px; color:rgb(51,51,51); font-family:'Helvetica Neue',HelveticaNeue,Helvetica,Arial,sans-serif; font-size:15px; line-height:22.5px"><br></p>🎜<pre class="brush:js;toolbar:false;">merge: function( first, second ) { var i = first.length, j = 0; //length属性为数字,则把second当做数组处理,没有length属性或者不为数字当做含有连续整型的属性对象处理{0:&quot;HK&quot;,1:&quot;CA&quot;} if ( typeof second.length === &quot;number&quot; ) { for ( var l = second.length; j &lt; l; j++ ) { first[ i++ ] = second[ j ]; } } else { while ( second[j] !== undefined ) { //把不为undefined的都合并到first中 first[ i++ ] = second[ j++ ]; } } first.length = i; //修正length属性,fisrt可能不为真正的数组类型 return first; },</pre>

위 내용은 jQuery에서 .makeArray()의 코드 읽기의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.