Original from: http://www.cnblogs.com/guorange/p/6668440.html
Recommended reading: Http://www.cnblogs.com/silin6/p/ArrayLike.html#muddle
1. What is a class array arraylikeHas the length property, and the other property (index) is a non-negative integer (the index in the object is treated as a string, which you can understand as a nonnegative integer string) without an array of methods
Class Array Example
var a = {' 1 ': ' GG ', ' 2 ': ' Love ', ' 4 ': ' Meimei ', length:5};
Array.prototype.join.call (A, ' + ');//' +gg+love++meimei '
//Non class array example
var c = {' 1 ': 2}; No Length property is not an array of classes
Common Arraylike have the following: Arguments nodelist stylesheetlist htmlcollection htmlformcontrolscollection (inheriting htmlcollection) Htmloptionscollection (inherited htmlcollection) htmlallcollection domtokenlist
2. Determine whether an object belongs to an array of classes
function Isarraylike (o) {
if (o && //O is not null, undefined, etc.
typeof o = = ' object ' && //O is an object
isfinite (o.length) && //O.length is a finite numbe R
o.length >= 0 && //O.length is non-negative
o.length===math.floor (o.length) && O.length is a integer
o.length < 4294967296) //O.length < 2^32 return
true; Then o array-like
else return
false; Otherwise it is not
}
3. What is the advantage of manipulating an array after converting it to a cluster?
Because the class array does not have a method of manipulating an array of arrays, it is convenient and efficient to call an array of classes to invoke these powerful methods, such as Shift,unshift,splice,slice,concat,reverse,sort, after they are converted to arrays. 4. Class arrays convert to array methods
Array.prototype.slice.call (Arraylike)
After the arguments is converted to an array, all elements after the first element are intercepted
var args = Array.prototype.slice.call (arguments,1);
First the result of Array.prototype.slice.call (Arraylike) is to convert the Arraylike object into an array object. So it's possible to call the array's methods directly behind it, such as
Array.prototype.slice.call (arraylike). ForEach (function (element,index) { //free to manipulate each element})
(1) Array.prototype.slice represents the slice method in the prototype of an array. Note that this slice method returns an array-type object.
Slice internal implementation
Array.prototype.slice = function (start,end) {
var result = new Array ();
Start = Start | | 0;
End = End | | This.length; This points to the invoked object, which, when called, can change the point of this, that is, the object being passed in, which is the key for
(var i = start I < end; i++) {
result.push (This[i]) ;
}
return result;
(2) can call the only method, so can not use [].call this form, to use [].slice. The first parameter of call means that the environment that actually invokes the slice becomes the Arraylike object. So it's like Arraylike also has an array of methods.
(3) Attach the common function of the array
var ToArray = function (s) {
try{return
Array.prototype.slice.call (s);
} catch (e) {
var arr = [];
for (var i = 0,len = S.length i < len; i++) {
//arr.push (s[i));
Arr[i] = s[i]; This is said to be faster than push
} return
arr;
5. Convert an array to a parameter list (class array)
When you invoke the apply method , the first argument is the object (this), and the second argument is an array collection, which illustrates an ingenious use of apply that converts an array to a parameter list by default ([param1,param2,param3] converted to PARAM1,PARAM2,PARAM3), this might take a little bit of effort to convert each item of an array into a list of parameters by using the program, So there are the following efficient methods.
You can refer to the previous Article JS function in the Apply (), call (), bind () method-(apply the other clever use (generally under what circumstances can use apply))
Reference sources
1.https://www.inkling.com/read/javascript-definitive-guide-david-flanagan-6th/chapter-7/array-like-objects
2.JavaScript Eccentricity 8: "Class Array Object"
A method for array of 3.js class arrays