Basic of JavaScript object-oriented technology (2)

Source: Internet
Author: User

Array
As we have mentioned, objects are unordered data sets, while arrays are ordered data sets. Data (elements) in arrays are accessed through indexes (starting from 0,
The data in the array can be any data type. The array itself is still an object, but due to many features of the array, the array and the object are usually different
Throughout this book, objects and arrays are often treated as distinct PES ypes.
This is a useful and reasonable simplification; you can treat objects and arrays as separate types
For most of your JavaScript programming. To fully understand the behavior of objects and arrays,
However, you have to know the truth: An array is nothing more than object with a thin layer of extra
Functionality. You can see this with the typeof OPERATOR: applied to an array value, it returns
The string "object". -- section7.5 ).
To create an array, you can use the "[]" operator or the Array () constructor to create a new one.

JS Code
  1. VaRArray1 = [];// Create an empty array
  2. VaRArray2 =NewArray ();// Create an empty array
  3. Array1 = [1,"S", [3, 4], {"Name1":"Name1"}];//
  4. Alert (array1 [2] [1]);// 4 access the array elements in the array
  5. Alert (array1 [3]. name1 );// Access objects in the Array Using name1
  6. Alert (array1 [8]);// Undefined
  7. Array2 = [,];// If no value is entered and only a comma is used, the element at the corresponding index is undefined.
  8. Alert (array2.length );// 3
  9. Alert (array2 [1]);// Undefined
 
VaR array1 = []; // create an empty array var array2 = new array (); // create an empty array array1 = [1, "S", [3, 4], {"name1": "name1"}]; // alert (array1 [2] [1]); // 4 access the array element alert (array1 [3]. name1); // name1 access the object alert (array1 [8]) in the array; // undefinedarray2 = [,]; // if no value is entered, only Commas are allowed, the element at the corresponding index is undefinedalert (array2.length); // 3 alert (array2 [1]); // undefined

When using new array () to create an array, you can specify a default size. The value is undefined, and you can assign values to them later.
The length of the array in javascript can be changed at will, and the content in the array can also be changed at will. Therefore, the initialization length is actually
There is no binding force on the array. For an array, If you assign a value to an index that exceeds its maximum length, the length of the array is changed, and no value is assigned to the index.
The value of the index is undefined. See the example below.

JS Code
    1. var array = New array (10 );
    2. alert (array. length); // 10
    3. alert (array [4]); // undefined
    4. array [100] = "100th" ; // This operation changes the length of the array, set the value of the 10-99 index to undefined
    5. alert (array. length); // 101
    6. alert (array [87]); // undefined
VaR array = new array (10); alert (array. length); // 10 alert (array [4]); // undefinedarray [100] = "100th"; // This operation changes the length of the array, at the same time, set the value corresponding to the 10-99 index to undefinedalert (array. length); // 101 alert (array [87]); // undefined

You can use the delete operator to delete the elements of an array. Note that this deletion only sets the elements of the array at this position as undefined, and the length of the array has not changed.
We have used the Length attribute of the array. The Length attribute is a read/write attribute, that is, we can change the Length attribute of the array.
The length of the array is arbitrarily changed. If the length is set to a value smaller than the length of the array, the index value greater than length-1 in the original array will be deleted. If the length is
If the value is greater than the length of the original array, the value between them is set to undefined.

JS Code
  1. VaRArray =NewArray ("N1","N2","N3","N4","N5");// Array of Five Elements
  2. VaRAstring ="";
  3. For(VaRI = 0; I <array. length; I ++ ){// Loop array element
  4. Astring + = array [I];
  5. }
  6. Alert (astring );// N1n2n3n4n5
  7. DeleteArray [3];// Delete the value of the array element
  8. Alert (array. Length +"_"+ Array [3])// 5_undefined
  9. Array. Length = 3;// Reduce the length of the array
  10. Alert (array [3]);// Undefined
  11. Array. Length = 8;// Expand the length of the array
  12. Alert (array [4]);// Undefined
 
VaR array = new array ("N1", "N2", "N3", "N4", "N5"); // array of the Five Elements var astring = ""; for (VAR I = 0; I <array. length; I ++) {// loop array element astring + = array [I];} alert (astring); // n1n2n3n4n5delete array [3]; // Delete the value of the array element alert (array. length + "_" + array [3]) // 5_undefinedarray.length = 3; // reduce the array length by alert (array [3]); // undefinedarray. length = 8; // The length of the expanded array alert (array [4]); // undefined

For other methods of arrays, such as join/reverse, we will not give an example here.

Through the above explanation, we know that the attribute value of an object is obtained by the attribute name (string type), while the element of the array is obtained
Reference (integer type 0 ~~ 2 ** 32-1) to get the value. The array itself is also an object, so the object attribute operation is also fully suited to arrays.

JS Code
    1. var array = New array ( "No1" , "NO2" );
    2. array [ "po" ] = "props1" ;
    3. alert (array. length); // 2
    4. // For arrays, array [0] is the same as array ["0 (? Not sure. This is true during testing.)
    5. alert (array [0] + "_" + array [ "1" ] + "_" + array. po); // no1_no2_props1

arrays are also objects, so Arrays can have their own attributes, but attributes and values are not a concept. "No1" and "NO2" are both array values, array ["po"] adds an attribute to the array, and its length does not change.

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.