JavaScript Object-Oriented programming (2) array

Source: Internet
Author: User
Tags arrays javascript array

Perhaps you would wonder why object-oriented programming begins with arrays. This is because ... In the midst of all the relationships ... Hey, let's sell a few words here first to see what our familiar array is like in JavaScript.

1. Create an array

There are many ways to create arrays in JavaScript. For example, use the array function. But that's not what we're talking about right now. Now we use the simple square brackets "[]" method to create the array.

var objAyyar = []; // 1
var objAyyar = [2]; // 2
var objAyyar = ["a", "b", "c"]; // 3
var objAyyar = [new Date(), 123, "abc"]; // 4

Here are four statements that create arrays. Here are one by one explanations:

In the first sentence, create an empty array;

In the second sentence, create an array with an array element of only one 2;

In the third sentence, create an array whose elements are initialized to "a", "B", "C" respectively;

To create an array where the first element is a date type object, the second element is the number 123, and the third element is the string "abc".

Recall that in the Java or C + + language, an array is a collection of elements with the same data type. For example, use the following statements in the Java language

int[] array = new INT[10];

An array of elements that can be placed into 10 int types is created. A big difference between arrays and other types of collections is that an array can only hold elements of the same data type (except for collections that use generics). But how can a JavaScript array hold different types of elements, as in the fourth sentence above? This is because JavaScript is a weakly typed language, with no large data type differences, so the elements of an array can be placed in different types.

2. Operation Array

An array is an ordered collection of elements. The elements in the array are ordered, which enables you to access each element in the array by subscript. Also, the JavaScript array is quite flexible. After you get used to the Java or C + + array, you may not be accustomed to the JavaScript array. To some extent, this array can be called a dynamic array. Look at this piece of code:

var arr = [1, 2, 3, 4, 5];
alert(arr.length); // 数组长度为5
alert (arr[3]); // arr[3] = 4
arr[9] = 10;    // 改变了数组的长度为10
alert(arr [7]);
alert(arr.length);

First create an array arr, you can see that its length is 5,arr[3] is 4. These are all very common. So the third sentence, arr[9] = 10; it's kind of interesting--in Java, this operation will cause an exception to the array's bounds, which is extremely dangerous in C + +. But in JavaScript, such operations are normal-you can change the size of the array dynamically! Although you don't have that much length in creating an array, you can specify it after you create it! At this time the Arr.length has automatically become 10. So, arr[7] what will it be? After running the code we will see that arr[7] is undefined. In other words, although arr[9] has a value, but the elements from arr[5 to arr[8] are undefined, that is, undefined. If you ask JavaScript, why not give an initial value? Oh, spare it! JavaScript doesn't know what value you want it to initialize! What if it's wrong? Let's just leave it.

var arr = [1, 2, 3, 4, 5];
alert(arr.length); // 数组长度 为5
delete arr[3]; // 删掉第4个元素
alert(arr.length); // 长度不变
alert (arr[3]); // arr[3] = undefined
arr.length = 4; // 缩短长度
alert(arr[4]);
arr.length = 10; // 增加长度
alert(arr[6]);

The code above is also interesting: you can delete any one of the array elements by using the delete operator, but the length does not change.

The Java array also has a length property, which is used to display the lengths of the array. An array of JavaScript also has this attribute. However, unlike Java, the length property of the latter is not read-only! You can set the value of the length property of the array arbitrarily, whether it is enlarged or shrunk! Just as the above code shows, after changing length, the elements that are out of bounds or previously undefined elements will become undefined. That is, when length is greater than the original length, the element from the original length to the length-1 becomes undefined, and when length is less than the original length, the element from length to the original length-1 will also be cleared to be set to undefined.

3. Non-numeric subscript?

If the dynamic Length property is not flexible enough, the JavaScript array has additional capabilities.

Have you ever seen an array subscript with a string? Java line? C + + OK? JavaScript is fine! Look at the following statement:

var arr = [1, 2, 3];
alert(arr[1] == arr["1"]);
arr["js"] = 4;
alert (arr["js"]);

The statements above see that arr[1] and arr["1" are actually the same effect! What the hell is going on here? We use the following statement to verify:

alert(1 == "1"); // true
alert(1 === "1"); // false

Because JavaScript is a weakly typed language, JavaScript converts it to the desired type as much as possible when using a variable. For example, if a number is required below the array, a string will be provided to try to convert the string to a number. Here the "1" is successfully converted to the number 1, so the statement is set up. This is the reason for returning true using the = = operator. the = = = operator does not allow such type conversions, so returns false.

So, this arr["JS"] how can also set up? That's not the problem. In other words, JavaScript actually allows strings to be labeled as numbers. This is perfectly legal in JavaScript.

Source: http://devbean.blog.51cto.com/448512/163574

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.