You can use Array. prototype. slice to convert an object with the length attribute to a real Array. This article will answer the last question about pseudo arrays mentioned in the general circular Traversal method forEach in javascript.
What is a pseudo array?
You can use Array. prototype. slice to convert an object with the length attribute to a real Array.
There are many such objects, especially arguments objects, as well as getElementsByTagName, document. childNodes, and so on. All of them return NodeList objects which belong to pseudo arrays.
We can use Array. prototype. slice. call (fakeArray) to convert a pseudo Array to a real Array object.
Here is an example:
The Code is as follows:
Var fakeArray01 = {0: 'A', 1: 'B', length: 2}; // This is a standard pseudo array object.
Var arr01 = Array. prototype. slice. call (fakeArray01 );
Alert (arr01 [0]); //
Var arr02 = []. slice. call (fakeArray01 );
Alert (arr02 [0]); //
Slice can be used to obtain array fragments. It returns a new array without modifying the original array.
In the example, we can see that fakeArray is successfully converted into an Array object. Maybe you want. prototype. slice. the call statement is unfamiliar. In fact, we can also use []. slice. the call method achieves the same effect. Why do we need to implement it in prototype mode? The answer is to execute the program in prototype mode with higher efficiency and more beautiful code.
Implementation of pseudo Array
Let's take a closer look at the implementation of pseudo array.
Let's look at some special use cases:
The Code is as follows:
Var fakeArray01 = {a: 'A', B: 'B', length: 2}; // No value corresponding to the length subscript
Var arr01 = Array. prototype. slice. call (fakeArray01 );
Alert (arr01 [0]); // undefined
Var fakeArray02 = {0: 'A', 1: 'B', length: 'num'}; // length is not a numerical value.
Var arr02 = Array. prototype. slice. call (fakeArray02 );
Alert (arr02 [1]); // undefined
Similarly, fakeArray01 and fakeArray02 are converted into real arrays, but the values in the arrays are undefined.
View V8 engine array. js To simplify the internal implementation of slice as follows:
The Code is as follows:
Function slice (start, end ){
Var len = ToUint32 (this. length), result = [];
For (var I = start; I <end; I ++ ){
Result. push (this [I]);
}
Return result;
}
We can see that slice does not need this to be of the array type, but only needs to have the length attribute. And the length attribute can not be of the number type. If the value cannot be converted, ToUnit32 (this. length) returns 0.
According to the above conclusions, we can conclude that fakeArray01 is converted to an array with lenth 2, its values are initialized to undefined, and fakeArray02 is converted to an array with length 0, undefined is returned for elements with a natural access subscript of 1.
IE Problems
All the problems can be explained for the standard browser slice implementation, but IE encountered problems when handling NodeList. In IE, NodeList cannot be converted to a real array, and an error occurs. Why? Strictly speaking, an abstract class Arraioid is defined in IE. Both Array and Arguments inherit this class, so slice can be used. However, DOM objects are connected to JScript through COM, which is invalid during slice detection.
Jquery and pseudo Array
Jquery uses a large number of pseudo arrays internally. It can be said that the entire Jquery object is built on the basis of the pseudo array, so let's look at some practical use of Jquery:
The Code is as follows:
FakeArray