Javascript Object-Oriented Programming (2): Array

Source: Internet
Author: User
Tags javascript array
Maybe you may wonder why Object-Oriented Programming starts from arrays? This is because ...... The relationships between them ...... Hey, here we will sell a token first. Let's take a look at what the familiar array looks like in JavaScript. 1. Create an array There are many methods to create arrays in JavaScript. For example, use the Array Function. But this is not what we want to talk about now. Now we use the simple square brackets "[]" to create an array. Var objAyyar = []; // 1
Var objAyyar = [2]; // 2
Var objAyyar = ["a", "B", "c"]; // 3
Var objAyyar = [new Date (), 123, "abc"]; // 4 there are four statements for creating arrays. The first sentence is to create an empty array. The second sentence is to create an array with only one element. The third sentence is to create an array, the elements of the array are initialized to "a", "B", "c" respectively. In the fourth sentence, an array is created. The first element is a Date object, the second element is the number 123, and the third element is the string "abc ". In Java or C ++, arrays are collections of elements of the same data type. For example, the following statement int [] array = new int [10] in Java will be used to create an array of 10 int-type elements. A major difference between arrays and other types of sets is that an array can only store elements of the same data type (except for a set of generic types ). However, in the fourth sentence above, how can JavaScript arrays store different types of elements? This is because JavaScript is a weak type language and there is no big difference in data types, so the elements of the array can be put into different types. 2. Operation ArrayAn array is an ordered set of elements. The elements in the array are ordered, so that each element in the array can be accessed through subscript. In addition, JavaScript arrays are quite flexible. When you get used to arrays of Java or C ++, you may not get used to arrays of JavaScript. To a certain extent, this array can be called a dynamic array. See the following code: var arr = [1, 2, 3, 4, 5];
Alert (ARR. Length); // The array length is 5
Alert (ARR [3]); // arr [3] = 4
Arr [9] = 10; // The array length is changed to 10.
Alert (ARR [7]);
Alert (ARR. Length); first create an array arr. You can see that its length is 5, and ARR [3] is 4. These are common. So the third sentence, arr [9] = 10; is a bit interesting -- in Java, this operation will cause an exception in the array out of bounds. In C ++, this operation is extremely dangerous. But in Javascript, this operation is normal-you can dynamically change the size of the array! Although you didn't have such a large length when creating an array, you can specify it after it is created! At this time, arr. length is automatically changed to 10. So what will arr [7] Be? After running the code, we can see that arr [7] is undefined. That is to say, although arr [9] has a value, the elements from arr [5] To arr [8] are undefined, that is, undefined. If you ask JavaScript, why not give an initial value? Alas, let it go! Javascript doesn't know what value you want it to initialize! What if it is wrong? Just stop ...... VaR arr = [1, 2, 3, 4, 5];
Alert (ARR. Length); // The array length is 5
Delete arr [3]; // deletes 4th elements.
Alert (arr. length); // The length remains unchanged.
Alert (arr [3]); // arr [3] = undefined
Arr. length = 4; // shorten the length
Alert (arr [4]);
Arr. length = 10; // increase the length
Alert (arr [6]); the above Code is also interesting: You can use the delete operator to delete any array element, but the length does not change. The Java array also has a length attribute to display the length of the array. The array of JavaScript also has this attribute. However, unlike Java, the length attribute of the latter is not read-only! You can set the length attribute value of the array at will, whether it is extended or reduced! As shown in the code above, after length is changed, the elements that are out of the border or that are not previously defined will become undefined. That is to say, when length is greater than the original length, all elements from the original length to length-1 will become undefined; When length is less than the original length, the elements from length to original length-1 are also cleared and set to undefined. 3. Non-numeric subscript?If the dynamic length attribute is not flexible enough, the JavaScript array has other capabilities. Have you ever seen strings used as the array underlying mark? Java? C ++? JavaScript! Take a look at the following statement: var arr = [1, 2, 3];
Alert (arr [1] = arr ["1"]);
Arr ["js"] = 4;
Alert (arr ["js"]); The preceding statement shows that arr [1] and arr ["1"] are actually the same! What's going on? We use the following statement to verify: alert (1 = "1"); // true
Alert (1 = "1"); // false when using a variable, javascript will try to convert it to the required type. For example, if a number is required under an array, a string is provided to convert the string to a number. The "1" here is successfully converted to the number 1, so this statement is true. This is why the = Operator returns true. The ===operator does not allow such type conversion, so false is returned. So how can this arr ["JS"] be established? This is not the problem above. That is to say, JavaScript allows strings to be used as numbers. This is completely legal in 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.