Table of contents
2. The relationship between objects and arrays
1. Create objects and arrays
create object
let obj1 = {}
let obj2 = new Object()
create array
let arr1 = []
let arr2 = new Array()
2. The relationship between objects and arrays
There is a saying that everything is an object. JavaScript has prototype inheritance, and all JavaScript built-in constructors inherit from Object.prototype. Under this premise, it can be understood that new objects (new Array and [ ]) created using built-in object methods all have the property of Object.prototype.
3. Arrays and pseudo-arrays
An array is essentially an ordered collection. The elements of the array can be of any type, but generally when we set up a new array, we keep the elements in the array with the same type.
Arrays have a length, and the length is variable. The length of the array length property is a built-in property, all arrays have a length property.
Pseudo-array is an object object, which has a length attribute, generally subscripted as an attribute. For example, arguments, and the list of node objects obtained in dom is also a pseudo-array.
How to convert a pseudo-array into a real array
function fn1(a,b,c){
return Array.prototype.slice.call(arguments)
}
let arr = fn1(1,2,3)
console.log(arr) //[1,2,3]
function fn2(...arguments){
return arguments
}
let newArr = fn(1,2,3)
console.log(newArr) //[1,2,3]
Arrays can use all methods of arrays, but pseudo-arrays cannot directly use methods of arrays. You need to convert pseudo-arrays into real arrays before you can use all methods of arrays.