{JS} objects similar to Arrays

Source: Internet
Author: User

A js array is special because
1. Its length attribute has some special behaviors:
1) when a new element is added to the List, its value is automatically updated;
2). Setting this attribute can expand or truncate the array.
2. JS arrays are also Array instances. Different Array methods can be called through them.

1 and 2 are unique features of JS arrays, but they are not the most basic features of defining an array. any object with a length attribute and corresponding non-negative integer attribute is called an array similar to an array.

Objects similar to arrays do not frequently appear, and they cannot call the array method or expect special behavior through the length attribute. 1) 2 ), however, you can still use code that traverses a real array to traverse them. in fact, many array algorithms are applicable to objects similar to arrays and real array objects, as long as you do not try to add elements to arrays or change the length attribute, you can treat objects like arrays as real arrays.

Create an array like this and traverse the array like this:
Var a = {};

Var I = 0;
// A small bug was introduced accidentally.
While (I ++ <10 ){
A [I] = I * I;
}
While (I <10 ){
A [I] = I * I;
I ++;
}
A. length = I;

Var total = 0;
For (var j = 0, len = a. length; j <len; j ++ ){
Total + = a [j];
}

In particular, the Arguments object in the function is an object similar to an array, and the DOM node list returned by getElementsByTagName () is also an object similar to an array.


By bill200711022

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.