Home > Web Front-end > JS Tutorial > body text

Code reading of .makeArray() in jQuery

黄舟
Release: 2017-07-19 09:41:15
Original
1133 people have browsed it

jquery’s makeArray function can convert an array-like object into an array. The official API explanation and test examples are here (Convert an array-like object into a true JavaScript array.) So what is an array-like object? ( array-like object ) This is the definition of Arrary-Like

Array-Like Object

Either a true JavaScript Array or a JavaScript Object that contains a nonnegative integer 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 property will trigger the Array-Like behavior.

contains the length attribute, and the value is not negative. The attribute can be accessed according to the subscript. Common array-like objects include aruments, NodeList


// 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 'length'
			// 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 === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) ) {
				push.call( ret, array );
			} else {
				//调用merge把类数组array合并到ret
				jQuery.merge( ret, array );
			}
		}

		return ret;
	}
Copy after login

The jquery.type called here

jquery.isWindow() 1.7.1 It is judged based on whether it contains the setInterval attribute "setInterval" in obj, and the subsequent layout is judged by the window attribute obj == obj.window


isWindow:function(obj){
			return obj && typeof obj === "object" && "setInterval" in obj;
			//1.7.2: return obj != null && obj == obj.window;
		},
Copy after login


The official explanation of the called jQuery.merge(frist,second) is here. The function of this function is to merge the second array or array-like object into the first one.


merge: function( first, second ) {
		var i = first.length,
			j = 0;
		//length属性为数字,则把second当做数组处理,没有length属性或者不为数字当做含有连续整型的属性对象处理{0:"HK",1:"CA"}
		if ( typeof second.length === "number" ) {
			for ( var l = second.length; j < l; j++ ) {
				first[ i++ ] = second[ j ];
			}

		} else {
			while ( second[j] !== undefined ) { //把不为undefined的都合并到first中
				first[ i++ ] = second[ j++ ];
			}
		}

		first.length = i; //修正length属性,fisrt可能不为真正的数组类型

		return first;
	},
Copy after login

The above is the detailed content of Code reading of .makeArray() in jQuery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!