What is the difference between array and class array objects (Array-like Object) in javascript?

Array and class array objects in javascript (Array-like Object), has three problems:

1, what"s the difference between array and class array objects?
2. How to generate class array objects?
3. How do I know something is an array object?

Please tell me, thank you

Jul.13,2021

Hello friend, the following is my understanding, I hope it will be helpful to you!

1. The class array has the length attribute, and other objects whose attributes are non-negative integers . The difference is that the class array has no array methods, such as push, and so on.

2. How to generate class arrays? Referring to the characteristics of 1 , we can define an array of classes as follows:

let person = {
    0: "",
    1: "18",
    length: 2
};

3. Judge whether it is a class array or not? According to the description of 1 , you can simply write the following code:

function isLikeArray(o) {
    if (typeof o === 'object' && isFinite(o.length) &&  o.length >= 0 && o.length < 4294967296){
        // 4294967296: 2^32
        return true
    } else {
        return false
    }
}

4. The commonly used class array is converted to an array, usually using the following two methods:

Array.prototype.slice.call(person);

or

Array.from(person);

Let's not introduce the

array, just talk about array-like objects.

Class array object:
arguments object
NodeList (such as the dataset obtained by document.getElementsByClassName ('a')
typedArray
.

the main difference:
class array objects cannot call methods on array prototypes. is that xx.push (), xx.slice (), xx.indexOf (), and so on, cannot be used.

how to tell if it's a class array object:
you don't have to go to the area to classify array objects, except for the above two. Just distinguish between arrays and objects.


< hr > You are very observant when you can ask this question.

generally speaking, we all know that arguments is a class array object. But I don't really know what a class array object is and why arguments belongs to a class array object.

what's the difference between array and class array objects?

they are all accessible to elements through an index, and all have length attributes; the difference is that class array objects do not have array methods.

how are class array objects generated?

if you want to know how to generate a class array object, you must first understand what a class array object is .

in the new version of the ECMA-262 document, we can see that there is already a related definition .

it must be an object
it has length attribute

so, an object can be treated as a class array object as long as it has the length attribute.

this explains how class array objects are generated: declare an object, set its length property, and you produce a class array object.

how do I know something is an array object?

judgment criteria: 1, it is an object; 2, it has length attribute.

expand here--

by looking at the ECMA-262 document, you can see that there is also CreateListFromArrayLike (argArray) fromarraylike" rel=" nofollow noreferrer "> built-in function , which is used to receive an array object and convert it into an array.

We cannot call this function externally, but we can call it indirectly through apply () . I think this function can indirectly prove whether a variable belongs to a class array object. For example, you can convert parameters to an array through Array.apply (null, arguments) , where arguments is a class array object.

if you pass a string to it for us, such as Array.apply (null, '123') , it will throw an error:

Uncaught TypeError: CreateListFromArrayLike called on non-object

so a string is not an array-like object.

you may wonder why you can use Array.from () to convert strings into arrays if the string is not an array-like object. Isn't the purpose of Array.from () to convert array-like objects into arrays?

the answer lies in the fact that Array.from () internally converts a string into a string object, which is the class array object.

you are welcome to follow the personal official account "front and rear test questions with answers". There happens to be an article discussion on this issue. For more details, please refer to the official account article, which will be listed here for your reference.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e4ba0e-593d2.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e4ba0e-593d2.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?