Class array of JavaScript

Source: Internet
Author: User

Class Array Object Ah, what difference does it have with a real array object? Can't say that old Maitai, only know the function of arguments object is a class array object, also has the length property, other? Dry Goods on the ministry:

First, let's talk about arrays:

1, the Length property is automatically updated when a new element is added to the list.

2, set length to a smaller value to truncate the array.

3, inherit some useful methods from Array.prototype.

4, its Class property is "Array".

These class array objects can be traversed, but cannot use array methods, nor do you expect any special behavior of the length property to see how the class array object is generated?

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

Here actually generated an object a, which has key,value key value pairs, the key value is from 0 to 9, there is a length property, the length property is defined, unless manually updated, otherwise unchanged.

The arguments object of the previous function is a class array object, and in client JavaScript, some DOM methods, such as document.getElementsByTagName (), also return the class array object. How do I detect a class array object?

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

As if the array also conforms to this, because the class array object does not inherit Array.prototype, should add Object.prototype.toString.call (o)!== ' array ', is a class array object.

Focus!!! Come and come!!!

Although the class array object does not inherit Array.prototype, there is nothing join,slice,pop,push and so on, but forgot the use of call and apply artifact? The code is as follows:

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 true array

Here's a question, how to convert a class array object into a real array object???

The answer is: a = Array.prototype.slice.call (a,0), and then the Array.isarray (a) detection type, true, haha

Part: string as an array

The string behaves like a read-only group, with square brackets [] In addition to accessing a single character with Charat ():

var s = ' test '; Undefineds.charat (0) = = = S[0]true

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

You can also apply the array method to the string above:

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

"T,e,s,t"
Note: strings are immutable values, as arrays are treated as read-only. Therefore, Push,sort,reverse,splice is invalid on the string. And try to use will error.

Class array of JavaScript

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.