Class array objects in JavaScript introduction to _javascript Tips

Source: Internet
Author: User

In JavaScript, an array is a special object whose property name is a positive integer, and its length attribute changes as the array member increases or decreases, and some methods are inherited from the array constructor for array manipulation. And for an ordinary object, if all of its property names are positive integers and have the corresponding length attribute, then although the object is not created by the array constructor, it still renders an array of behaviors, in which case these objects are referred to as "class arrays objects." The following is a simple class array object:

Copy Code code as follows:

var o = {0:42, 1:52, 2:63, Length:3}
Console.log (o);

Unlike ordinary objects, the class array object has an attribute: The operation method of the array can be applied on the class array object. For example, in the ECMAScript 5 standard, you can combine the above object o into a string using the following methods:
Copy Code code as follows:

Console.log (Array.prototype.join.call (o));//"42,52,63"

You can also use the slice () method on a class array object to get a child array:
Copy Code code as follows:

Console.log (Array.prototype.slice.call (O, 1, 2));//[52]

In a browser environment, the document.getElementsByTagName () statement returns a class array object. In a function call, the arguments variable within the function code (which holds the incoming argument) is also an array object of the class.

In the ECMAScript 5 standard, a strings string is a read-only class Array object:

Copy Code code as follows:

var s = "History";
Console.log (S[3]);//t
Console.log (Array.prototype.join.call (S, ""));//h i s t o r y

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.