What is an array of classes
? Broadly speaking, an object with a length property is an array of classes , but the class array, as its name implies, is just like an array but not an array, so it often does not have an array of methods, such as, forEach()
push()
, shift()
etc.
? In the JavaScript world there are many arrays of classes, such as,,, and NodeList
HTMLCollection
NameNodeMap
DOMTokenList
functions arguments
... They're all objects with length.
Class Array Conversion Group
? It says that an array of classes is just a way to look like an array but not a lot of arrays, so it's not easy to operate so we can convert them to arrays
// ES3,5Array.prototype.slice.call//or applyArray.apply(null,// apply的第二个参数接受一个数组或者类数组// ES6Array.from(arrlike)[...arrlike// 前提是这个arrlike得具备迭代器接口[Symbol.iterator]。而以上三种有length即可
? It was previously said that an array of classes is an object with the length property, which is not exactly defined. Because there's something in there that needs to be noticed, the array is labeled as a number , but as the previous definition of the class array has only the length property, it does not look like an array, and its properties may not be strings in numeric form. Of course, arrays can also define non-numeric types of strings as properties, after all, the array itself is an object, but it is mainly in the form of a numerical subscript to store data, and those attributes are not counted into length
? so when an array of classes is converted to arrays, the corresponding data is not stored in the created array if the attribute in the object is not a string in numeric form, or if the value is greater than or equal to length.
Vacancies are filled with undefined instead of empty units
ConstArrlike1= { 0: ' A ', 1: ' B ', ' 2 ': ' C ', length: 3 }Console.Log(Array. from(ARRLIKE1))//[' A ', ' B ', ' C ']ConstArrlike2= { 0: ' A ', ' A ': ' B ', ' 2 ': ' C ', length: 3 }Console.Log(Array. from(Arrlike2))//[' A ', Undefined, ' C ']ConstArrlike3= { 1: ' A ', length: 1 }Console.Log(Array. from(ARRLIKE3))//[undefined]
class arrays and their transformations