Home  >  Article  >  Web Front-end  >  Detailed explanation of javascript array_basic knowledge

Detailed explanation of javascript array_basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:33:101590browse

If you are an experienced developer, you may think this question is relatively simple, but sometimes, we find this question more interesting.

First let’s take a look at the definition of an 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.” , below we use objects to describe the definition of arrays:

Copy code The code is as follows:

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

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

Copy code The code is as follows:

var arr = {
"0" : "benjamin",
"1" : "zuojj",
"length" : 2
};

We know that in the Javascript language, an array is a special object. We can access the properties of the object using the method of accessing the array. At the same time, the array can also add properties like an object. Look at the following example:

Copy code 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);

Let’s take a look at the methods of arrays. Arrays have many operable methods, such as indexOf/slice/splice/sort, etc. We know that these methods actually exist in Array.prototype. Look at the example below:

Copy code The code is as follows:

var arr = ["benjamin", "zuojj"];
//Outputs: 1
console.log(arr.indexOf("zuojj"));
arr.indexOf = function(str) {
Return "It is customized indexOf!";
}
//Outputs: "It is customized indexOf!"
console.log(arr.indexOf("zuojj"));

In fact, we can overload all array methods using objects. See the push method example below:

Copy code The code is as follows:

var arr = {
length: 0,
Push: function(val) {
//Assignment
This[this.length] = val;
//Update array length
This.length = 1;
​​​​ //Return 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);

But there is one thing that cannot be re-implemented, the literal definition of an array:

Copy code The code is as follows:
var arr = ["benjamin", "zuojj"];

But we can use constructor instead:

Copy code The code is as follows:
var arr = new Array("benjamin", "zuojj");

If a literal definition of an array does not apply, then we can redefine the definition of the array in our own way.

Copy code The code is as follows:
var myArr = new CustomArray("benjamin", "zuojj");

Now you know how arrays work in JavaScript, I hope it will be helpful to everyone.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn