Class Array object in JavaScript

Source: Internet
Author: User

I knew long ago that I could convert arguments to an array: [].slice.call (arguments), because

Arguments is a class array object, so you can use it. But I never knew what it was called Class array objects (Array-like objects)

Today to see effective JavaScript has a section is devoted to this, feeling really too drag.

First look at some of the sample code I wrote:

a = "Hello"


[].map.call (A, (e)-> e.touppercase ()) # => [' H ', ' e ', ' l ', ' l ', ' O ']


[].reduceright.call (A, (ACC, E)-> ACC + e) # => ' Olleh '


B = {1: "A", 2: "B", 4: "C", Length:6}


[].reduce.call (b, (ACC, E)-> ACC + e) # => ' abc '

The first few are manipulating strings, well, strings can also be thought of as class array objects. But that B-object back there
is also a class array object.

The explanation in reading:

The

code is as follows:


So what exactly makes an object "Array-like"? The basic contract of


an Array object amounts to two simple rules.


It has an integer length in the range 0...2^32–1.


the Length property is greater than the largest index of the object.


an index is a integer in the range 0...2^32–2 whose string representation


is the key to a property of the object.

There are only these two simple rules.

So why arguments, strings, and the above B-objects can be thought of as class array objects?

They all have a valid length property (a positive integer between 0 and 2**32-1).
The value of the length property is greater than their maximum index.
Another example:

The

code is as follows:


B = {1: "A", 2: "B", 4: "C", Length:3}


[].reduce.call (b, (ACC, E)-> ACC + e) # => ' AB '

Well, it's wrong, it's ' ab ', because it violates the rules. 2:length property is 3,
The maximum index value is 4 larger than the length property. So the performance is not normal.

Too powerful, as if only to define an interface, as long as the interface is met, you can take advantage of all the methods of the array.

Well, not all the way, Array.prototype.concat.
is not available, because it is connected to two arrays, you are not an array must not be able to use it.

Another small problem is that the string creation is immutable (immutable), so how you toss it is immutable.

But the book simply does not explain why it fits these two conditions as a class array object, and the author of the book
It's a member of the ECMAScript committee, so it's basically believable. As for why meet these two conditions can be regarded as an array of objects, I do not know, Google search for a half-day also did not see any reasonable explanation.

The above mentioned is the entire content of this article, I hope you can enjoy.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.