4.1. Class Array Construction
As you can see from the previous section, the jquery constructor completes the lookup or transformation or other functionality, and the result is finding the element. DOM Tree Lookup, HTML conversion to DOM elements, or direct incoming DOM elements are just ways. Finding these elements means finding a place to store them.
The place where the ordered data is stored (the collection) is, of course, the array in JS. So how do you implement an array in the jquery inner surface? You can use the following methods:
jQuery.fn.prototype=new Array();
In the This.setarray (ARR) function in the previous section, add
Array.apply (this,arr);
If you want to be a little more perfect, add:
jQuery.fn.prototype.constructor=jQuery。
This allows us to inherit all of the attributes of the array, and we can extend the functionality of the array in the JQuery object. But jquery does not implement this internal collection by inheriting array. It uses the implementation of the Array-like object (see Javascript:the definitive Guide, 5th Edition7.8 section).
The object of an array of classes is still an object, just like an array. Arrays and objects are in fact no good big difference, ordered and unordered set is their difference. This difference is reflected in the length property of the array. When you add an element it automatically adds a relative number, and when you delete the element, it automatically subtracts the relative number.
Take a look at how jquery is implemented:
// 第一种情况 Handle $(DOMElement)单个Dom 元素,忽略上下文
if (selector.nodeType) { ②
this[0] = selector;
this.length = 1;
return this;
}
This is its first implementation method, through This[0] to directly set the first position of the DOM element, while setting the length=1. Here we can see that objects like arrays are in the form of key/value pairs. The JSON form above is {0:adom,length=1}. Here is an analysis of the array, which is inherited from the object. The final result of its [] interpretation can be regarded as an object of {} construction, where the index (such as 0.1,....) is used as the key of the object property when the [] or array is built. The value in the array as its corresponding value. Change the value of length at the same time. That is to say why essentially objects are not much different from arrays. In many of the source code, such as Yui in the form of objects to build multidimensional arrays.
this.setArray(jQuery.makeArray(selector));
is the second implementation of the method, the above implementation is a single element, the implementation of the collection of multiple elements. It first calls the Jquery.makearray (selector) static method to convert the collection (class array) into arrays.
It has been analyzed that both arrays and objects can take obj. [attr] In the form of a value corresponding to its key. For a collection or an array of classes, you must require it to implement the length property, and with lengths, then get the corresponding value from the 0~length-1 key property:
//把类数组的集合转换成数组,如果是单个元素就生成单个元素 的数组。
makeArray: function( array ) {
var ret = [];
if( array != null ){ var i = array.length;
//单个元素, 但window, string、 function有 'length'的属性,加其它的判断
if( i == null || array.split || array.setInterval || array.call )
ret[0] = array;
else//类数组的 集合
while( i ) ret[--i] = array[i];//Clone数组
}
return ret;
},
Generates a standard array, what is the next setarray to do?
// 把array-like对象的元素 全部push当前jquery对象。
setArray : function(elems) {
this.length = 0;//初始化长度,因为push会在原始的length++
Array.prototype.push.apply(this, elems);
return this;
},