An array of class

The class array is defined as an empty {} object with the length attribute, push method, and splice method

Case 1

function test(){ console.log(arguments); // arguments.push(7); } test(1,2,3,4,5,6,7);Copy the code

Case 2

Var obj = {" 0 ": 'a', / / attributes of the object can be any form of" 1 ", "b", "2" : 'c'} var arr = [' a ', 'b', 'c'].Copy the code

Example 3

var obj={
    "0":'a',
    "1":'b',
    "2":'c',
    "length":3,
    "push":Array.prototype.push,
    "splice":Array.prototype.splice
}
Copy the code

Class array: To have an index (number) attribute, you must have the length attribute, plus the length attribute can do a series of processing. If you add push to an object and add splice to it, it’s going to look like an array

When you push, the key point is length, which overrides the push thing

Implementation class array

Array.prototype.push = function(target){ obj[obj.length] = target; //this[this.length] = target; obj.length ++; }Copy the code

Example 4

var obj = { "2":"a", "3":"b", "length":2, "push":Array.prototype.push } obj.push('c'); obj.push("d"); // When pushing, the key point is length, which overwrites the push thingCopy the code

deformation

var obj = {
    "1":"a",
    "2":"c",
    "3":"d",
    "length":3,
    "push":Array.prototype.push
}
obj.push("b");
Copy the code

Example 5 iterate through all attribute fot in loops

var obj = { "0":"a", "1":"b", "2":"c", name:"abc", age:123, length:3, push:Array.prototype.push, Splice: Array. Prototype. For (var prop in obj) with a splice} {/ / traverse all attributes fot cycle in the console. The log (obj [prop]); }Copy the code