Home > Web Front-end > JS Tutorial > body text

Array and object in js

高洛峰
Release: 2017-02-28 14:16:11
Original
1191 people have browsed it

•Object type:

◦ Creation method:

/*new 操作符后面Object构造函数*/ 
var person = new Object(); 
person.name = "lpove"; 
person.age = 21; 
/*或者用对象字面量的方法*/ 
var person = { 
name: "lpove"; 
age : 21; 
}
Copy after login

•array type

◦ Create Method:

`var colors = new Array(“red”,”blue”,”yellow”);
Copy after login

• Differences and puzzles

◦ For example, there is an array a=[1,2,3,4], and There is an object a={0:1,1:2,2:3,3:4}, and then you run alert(a[1]). The results in the two cases are the same! This means that data collections can be represented by arrays or objects, so which one should I use?

I later learned that arrays represent collections of ordered data, while objects represent collections of unordered data. If the order of the data is important, use an array, otherwise use an object.

Of course, another difference between arrays and objects is that the data of the array does not have a "name" (name), while the data of the object has a "name" (name).

But the problem is that in many programming languages, there is something called an "associative array" (associative array). The data in this array has names.

• However, in "javascript DOM", we are not recommended to use associative arrays;

◦ Associative array:

var lpove = Array(); 
lpove[name] = "lei"; 
lpove[age] = 21; 
lpove[living] = true;
/*对象构造*/
  var lpove = Object();
    lpove.name = "lei";
    lpove.age = 21;
    lpove.living = true;
Copy after login

Because essentially the nature of the associative array you create is the property of the Array object

The above article discusses the difference between arrays and objects in js is all the content shared by the editor. , I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website.

For more articles related to arrays and objects in js, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template