Unlike the Java language, arrays in JavaScript have three attributes:
1. No type. The members of an array can be of any type, and the same array can consist of many different types of members.
2. Variable length. The length of an array can be dynamic, so there is no cross-border problem with array access in JavaScript.
3. Discontinuity. The position of the members in the array can be continuous (0, 1, 2, 3 ...), or it can be discontinuous. Any array has a property named length, which, in the case of contiguous array members, is consistent with the number of members of the array, and the length value is greater than the number of array members when the array member is not contiguous. Compared to successive arrays, the performance of the discontinuous array is poor.
Experiment:
Copy Code code as follows:
var o = ["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}, p, undefined, 99]
As you can see from the example above, JavaScript will return to undefined when you access the missing member for a discrete array. If the array is contiguous, but one of the members is undefined, the result of accessing the array is the same:
Copy Code code as follows:
var a = [N, "Sample Text", {x:88}, 99, undefined;
Console.log (a);//[42, "Sample Text", Object {x=88}, undefined, 99]
Arrays are discontinuous, members are missing, and arrays are contiguous, but members are undefined, and in both cases the results of accessing the array contents are the same. But there are some subtle differences between the two, mainly in the access to the array key:
Copy Code code as follows:
Console.log (4 in O);//false
Console.log (4 in a);//true
As you can see, in both cases, while the results of the access content are consistent, but its internal mechanism is completely different: a member is missing in the case of an array discontinuity, so when the member is accessed, JavaScript returns undefined, and all members exist in the continuous array. Only the value of some members is more special, for undefined only.
As you can see from the example above, the array in JavaScript is essentially just an object with a number key, and there is no difference between an object and a normal key value. In fact, when reading and writing an array, JavaScript attempts to convert the argument to a positive integer and, if the conversion succeeds, an array operation (which automatically updates the length property of the array) and, if it fails, converts the argument to a string and then reads and writes the normal object. Of course, in the implementation of the JAVASCRPT interpreter, there are many performance optimizations for this feature of the array as key, so if the key of the object is a number in the actual use, then the direct use of the array object will get more efficient results.
During the definition of an array, JavaScript allows an extra comma, and also allows the deletion of an array member between two commas:
Copy Code code as follows:
var x = [1,2,3,];//trailing comma'll 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
For the creation of arrays, JavaScript supports four different methods:
1. Use literal quantities, such as the bracket expressions in the preceding examples, to create an array object directly.
2. Use the array () constructor without passing in any parameters. In this case, an empty array is created with an effect equal to [].
3. Use the array () constructor to pass in a positive integer as the length of the array. In this case, JavaScript reserves the appropriate memory space to store the array. It is noteworthy that the key of the array is not defined at this time, that is, there are no members in the array. The effect is equivalent to that of [,,,,]
4. Use the array () constructor to pass in the members of the array.
Experiment:
Copy Code code as follows:
var z = new Array;//pre-allocate memory, but no index is defined yet.
Console.log (3 in z);//false
var m = new Array ("test", {k:99});
Console.log (m);//[42,,, "test", Object {k=99}]
In the ECMAScript 5 standard, you can use Array.isarray () to determine whether an object is an array:
Copy Code code as follows:
Array.isarray ([]);//true
Array.isarray ({});//false