Introduction to class array objects in JavaScript, javascript Array
In JavaScript, an array is a special object. Its property name is a positive integer and Its length attribute changes with the increase or decrease of array members, at the same time, some methods used for Array Operations are inherited from the Array constructor. For a common object, if all its property names are positive integers and corresponding length attributes exist, although this object is not created by the Array constructor, it still presents the behavior of arrays. In this case, these objects are called "class array objects ". The following is a simple array object of classes:
Copy codeThe Code is as follows:
Var o = {, 2: 63, length: 3}
Console. log (o );
Unlike normal objects, a class array object has a feature that can be used to operate Arrays on class array objects. For example, in the ECMAScript 5 standard, you can use the following method to merge the above object o into a string:
Copy codeThe Code is as follows:
Console. log (Array. prototype. join. call (o); // "42,52, 63"
You can also use the slice () method to obtain the sub-array on the class array object:
Copy codeThe Code is as follows:
Console. log (Array. prototype. slice. call (o, 1, 2); // [52]
In the browser environment, the document. getElementsByTagName () Statement returns an array object of classes. In a function call, the arguments variable in the function code (storing input parameters) is also an array object of classes.
In the ECMAScript 5 standard, string is a read-only array object:
Copy codeThe Code is as follows:
Var s = "History ";
Console. log (s [3]); // t
Console. log (Array. prototype. join. call (s, ""); // H I s t o r y