Class Array Object _javascript techniques 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:

Copy Code code as follows:

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:

Copy Code code 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 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:

Copy Code code 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

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.