JavaScript array-like

Array-like object, what is the difference between it and a real array object when asked? If you can’t say it, it’s too old. I only know that the arguments object of the function is an array-like object, and it also has a length attribute. What about the others? Dry goods are offered:

Let's talk about arrays first:

1. When a new element is added to the list, the length property is automatically updated.

2. Setting length to a smaller value will truncate the array.

3. Inherit some useful methods from Array.prototype.

4. Its class attribute is "Array".

These array-like objects can be traversed, but you can't use array methods, and you don't need to expect any special behavior of the length property. Let's see how array-like objects are generated, right?

 var a = {};
 var i = 0;
 while(i<10){
     a[i] = i*i;
     i++;
 }
 a.length = i;

 var total = 0;
 for(var j = 0;j<a.length;j++){
     total += a[j];
 }

In fact, an object a is generated here, which contains key and value key-value pairs. The key value is from 0 to 9, and there is a length attribute. This length attribute is defined and will remain unchanged unless it is manually updated.

The arguments object of the previous function is an array-like object. In client-side javascript, some DOM methods such as document.getElementsByTagName() also return an array-like object. How to detect array-like objects?

First make sure that it is an object, not null or undefined, its length is a finite non-negative integer, typeof o === 'object';

It seems that the array also conforms to this, because the array-like object does not inherit Array.prototype, it should be added with Object.prototype.toString.call(o) !== 'Array', which is an array-like object.

Focus! ! ! come come come! ! !

Although array-like objects do not inherit Array.prototype, there are no join, slice, pop, push, etc. methods, but have you forgotten the usage of call and apply artifacts? code show as below:

var a = {"0":"a","1":"b","2":"c","3":"d",length:4};
Array.prototype.join.call(a, '+'); // "a+b+c" 
Array.prototype.slice.call(a,0)    // ['a','b','c' ,'d'], a copy of the real array

Here is a question, how to convert an array-like object into a real array object? ? ?

The answer is: a = Array.prototype.slice.call(a,0), then use Array.isArray(a) to detect the type, true, hahahaha

Part two: Strings as arrays

Strings behave like read-only arrays, except to access individual characters with charAt(), you can also use square brackets []:

var s = 'test' ;
undefined
s.charAt(0) === s[0]
true

Square brackets instead of charAt() are more concise, readable, and efficient.

You can also apply array methods to strings:

Array.prototype.join.call(s,",")

"t,e,s,t"
Note: Strings are immutable values, read-only when treated as arrays. So, push, sort, reverse, splice, etc. are invalid on strings. And trying to use it will give an error.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325265315&siteId=291194637