Javascript array detail_basic knowledge-js tutorial

Source: Internet
Author: User
An array object is used to store a series of values in a separate variable name. We use the keyword new to create an array object. There are two ways to assign values to arrays. You can also use an integer independent variable to control the array capacity. If you are an experienced developer, you may think this problem is relatively simple, however, sometimes we may find this problem interesting.

First, let's take a look at the definition of the array:"An array is just a list of values which can be accessed by using an integer as the" key ". The list starts at 0 and goes up from there ."In the following example, we use an object to describe the definition of an array:

The Code is as follows:


Var arr = ["benjamin", "zuojj"];
// =>
Var arr = {
"0": "benjamin ",
"1": "zuojj"
};

Looking at the example above, I always feel that something is missing, OK, and the length of the array:

The Code is as follows:


Var arr = {
"0": "benjamin ",
"1": "zuojj ",
"Length": 2
};

We know that in Javascript, arrays are special objects. We can access the attributes of objects by accessing arrays. At the same time, Arrays can also be added as objects. See the following example:

The Code is as follows:


Var arr = {
"0": "benjamin ",
"1": "zuojj ",
"Length": 2
};
// Outputs: "benjamin"
Console. log (arr [0]);
// Outputs: 2
Console. log (arr. length );

Var arr = ["benjamin", "zuojj"];
Arr. url = "www.jb51.net ";
// Outputs: "www.jb51.net"
Console. log (arr. url );
// Outputs: 2
Console. log (arr. length );

Next let's take a look at the Array methods. There are many operational methods for arrays, such as indexOf/slice/splice/sort. We know that these methods actually exist in Array. prototype. See the following example:

The Code is as follows:


Var arr = ["benjamin", "zuojj"];
// Outputs: 1
Console. log (arr. indexOf ("zuojj "));
Arr. indexOf = function (str ){
Return "It is med indexOf! ";
}
// Outputs: "It is med indexOf! "
Console. log (arr. indexOf ("zuojj "));


In fact, we can use the object to overload all array methods. Let's look at the example of the push method below:

The Code is as follows:


Var arr = {
Length: 0,
Push: function (val ){
// Assign a value
This [this. length] = val;
// Update the array Length
This. length + = 1;
// Returns the length of the array.
Return this. length;
}
}
Arr. push ("zuojj ");
Arr. push ("benjamin ");
// Object {0: "zuojj", 1: "benjamin", length: 2, push: function}
Console. log (arr );

However, one cannot be implemented again. The literal definition of an array is as follows:

The Code is as follows:

Var arr = ["benjamin", "zuojj"];

However, we can use constructors instead:

The Code is as follows:

Var arr = new Array ("benjamin", "zuojj ");

If the array definition is not applicable to literal expressions, We can redefine the array definition in our own way.

The Code is as follows:

Var myArr = new CustomArray ("benjamin", "zuojj ");

Now you know how arrays in javascript work. I hope it will help you.

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.