Introduction to array features in JavaScript and javascript Array features

Source: Internet
Author: User

Introduction to array features in JavaScript and javascript Array features

Unlike Java, arrays in JavaScript have three features:

 

1. No type. The members of the array can be of any type, and the same array can also be composed of many different types of members.
2. Variable Length. The length of the array can be dynamically changed, so there is no out-of-bounds problem of array access in JavaScript.
3. Non-continuity. The positions of Members in the array can be continuous (0, 1, 2, 3 ...), It can also be discontinuous. Any array has an attribute named length. When the array members are continuous, the length value is consistent with the number of array members. When the array members are not continuous, the length value must be greater than the number of array members. Compared with continuous arrays, the read/write performance of discontinuous arrays is worse.

 

Lab:


Copy codeThe Code is as follows:
Var o = [42, "Sample Text", {x: 88}]; // JavaScript array is un-typed.
Console. log (o); // [42, "Sample Text", Object {x = 88}]
O [3] = 27; // JavaScript array is dynamic.
Console. log (o); // [42, "Sample Text", Object {x = 88}, 27]
O [5] = 99; // JavaScript array is sparse.
Console. log (o); // [42, "Sample Text", Object {x = 88}, 27, undefined, 99]


From the above example, we can see that for Discontinuous arrays, when accessing the missing member, JavaScript will return undefined. If the array is continuous but a member is undefined, the result of accessing the array is the same:


Copy codeThe Code is as follows:
Var a = [42, "Sample Text", {x: 88}, 27, undefined, 99];
Console. log (a); // [42, "Sample Text", Object {x = 88}, 27, undefined, 99]


The array is discontinuous and has missing members, which is continuous with the array but has undefined members. In both cases, the results obtained by accessing the array content are the same. However, there are some minor differences between the two, mainly in the access to the array key:


Copy codeThe Code is as follows:
Console. log (4 in o); // false
Console. log (4 in a); // true


We can see that although the results of the access content are the same in these two cases, the internal mechanism is completely different: When the array is not continuous, a member is missing, therefore, when accessing this Member, JavaScript returns undefined. In the case of array continuity, all members exist, but some members have special values, which are undefined.

From the above example, we can also see that the array in JavaScript is essentially an object that uses numbers as the key, and there is no difference with normal key-value pairs. In fact, when reading and writing an array, JavaScript will try to convert the parameter to a positive integer. If the conversion is successful, the array operation will be performed (The length attribute of the array will be automatically updated ), if it fails, convert the parameter to a string and then perform the read and write operations on the common object. Of course, in the implementation of the JavaScrpt interpreter, we have made a lot of performance optimization for the array feature of using numbers as keys. Therefore, in actual use, if the object's keys are all numbers, then directly using the array object will produce more efficient results.

When defining an array, JavaScript allows redundant commas (,) or missing array members between two commas:


Copy codeThe Code is as follows:
Var x = [1, 2, 3,]; // trailing comma will be omitted.
Console. log (x. length); // 3

Var y = [1, 3]; // member can be missed.
Console. log (y); // [1, undefined, 3]
Console. log (1 in y); // false
Console. log (y. length); // 3


JavaScript supports four methods for creating Arrays:

1. directly create an array object using the literal (such as the brackets expression in the preceding examples.
2. Use Array () to construct a function without passing in any parameters. In this case, an empty array is created, which is equivalent.
3. Use the Array () constructor to input a positive integer as the length of the Array. In this case, JavaScript reserves the corresponding memory space to store this array. It is worth noting that the keys of the array are not defined at this time, that is, the array does not contain any members. The effect is equivalent [,].
4. Use the Array () constructor to input the members of the Array.

Lab:


Copy codeThe Code is as follows:
Var z = new Array (10); // pre-allocate memory, but no index is defined yet.
Console. log (3 in z); // false

Var m = new Array (42, 33, 99, "test", {k: 99 });
Console. log (m); // [42, 33, 99, "test", Object {k = 99}]

In the ECMAScript 5 standard, you can use Array. isArray () to determine whether an object is an Array:
Copy codeThe Code is as follows:
Array. isArray ([]); // true
Array. isArray ({}); // false

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.