An array of JavaScript learning notes

Source: Internet
Author: User

Definition of the array:

1,var arr=new Array (); --the array is a special object, and the return value of the TypeOf is Object

ARR[0]

ARR[1]

...

2,var arr=new Array ("", "")--here directly to the array element if the number needs to be more than 2, otherwise only one value will be resolved the length of the group

3,var arr=["", "" "]

4,var arr=[]

arr[0]=

arr[1]=

...

It is recommended to always create arrays directly with square brackets

Any one type of data can be put into an array. If the elements of an array are still arrays, a multidimensional array is formed.

The special body of an array now, its key is by default a sequential integer (0,1,2 ... ), so the array does not have to specify a key name for each element, and each member of the object must specify the key name. In addition, the object identifies the key name as a string, and the non-string key name is converted to a string, so the member of the array can be read using a numeric value or a string as the key name.

is also set when assigning values, that is, if a value can be converted to an integer, the value is the key name, and the corresponding integer is the key name.

Skipping an undefined value when specifying an array returns a number and returns how many values are skipped.

An object has two methods for reading members: the point structure (object.key) and the square bracket structure (Object[key]).

However, for numeric key names, the point structure cannot be used, and the arr.0 is not legal, because individual numbers cannot be identifiers (identifier).

Therefore, array members can only be expressed in square brackets arr[0] (square brackets are operators and can accept numeric values).

Property:

Length Property:

1.length can hold the number of array elements. JS uses 32-bit integers, so the maximum number of members of an array is (2^32-1);

2. The length property of an array differs from the length property of an object, as long as it is an array, there must be a length property, and the object may not have one. Also, the length property of the array is a dynamic value equal to the largest integer in the key name plus 1.

3. The value of the array is always greater than the last member's key value of +1, and how many real members are not affected. Indicates that the array is a dynamic data structure that can be added or subtracted from the array at any time.

4. The length property of the array can also be set to the lengths of the array, and if the members of the array are longer than the length value, the array is automatically shortened to length-1;

5. There is one way to empty an array: set length=0

6. If the set length is greater than the current number of elements, the number of members of the array is increased to this value, and the new position is filled in with undefined.

7. If you set length to an illegal value, JavaScript will give an error.

Array of Slots:

1. When there is an empty element somewhere in the array (for example, there is no value between the two commas, or the value is undefined), we call the array with a vacancy (hole). Vacancy generation undefined

2. If there is a comma behind the last element, no vacancy is generated. In other words, there is no comma, the result is the same. However, IE 8 and the following versions do not support this syntax rule, will be error.

3. Use the Delete command to delete a value that creates an empty space. But does not affect the value of length, will generate a undefined;

Therefore, the length property does not filter the value of undefined. Therefore, use the Length property for array traversal and be very careful.

4. There is a slight difference in whether the empty space is generated by a null value or explicitly set to undefined. If generated by a null value, the empty space is skipped by using the array's foreach method or the for...in structure for traversal.

If the empty space is generated by explicitly defining the undefined, the traversal will not be skipped.

Array () Arrays object:

Array.isarray () method: to determine whether an array, to compensate for the deficiencies of the TypeOf

To invoke an array instance, the method can be written as a [].method.call (call object, parameter) Form, or Array.prototype.method.call (call object, parameter).

valueOf () method: returns the array itself

toString () method: converts an array into a string, and a two-dimensional array becomes a string in order

An array of JavaScript learning notes

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.