The following small series will introduce the differences between arrays and objects in js. I think this is quite good. Now I will share it with you and give you a reference. Let's take a look at it together. • object Type:
How to Create a producer:
/* Object constructor after the new operator */var person = new Object (); person. name = "lpove"; person. age = 21;/* or use the object literal Method */var person = {name: "lpove"; age: 21 ;}
• Array type
How to Create a producer:
`var colors = new Array(“red”,”blue”,”yellow”);
• Differences and puzzles
For example, a struct has an array a = [,], and an object a = {,}. Then you run alert (a [1]), in both cases, the running results are the same! This means that the data set can be represented by arrays or objects. Which one should I use?
I later learned that arrays represent the set of ordered data, while objects represent the set of unordered data. If the order of data is important, use an array. Otherwise, use an object.
Of course, another difference between an array and an object is that the array data does not have a "name" and the object data has a "name ).
But the problem is that many programming languages have something called the associative array. The data in this array is named.
• In javascript DOM, correlated arrays are not recommended;
Correlated array:
Var lpove = Array (); lpove [name] = "lei"; lpove [age] = 21; lpove [living] = true; /* Object Construction */var lpove = Object (); lpove. name = "lei"; lpove. age = 21; lpove. living = true;
Because, in essence, the properties of the associated Array you created are the attributes of the Array object.
The difference between array and object in js is that the small Editor shares all the content with you. I hope to give you a reference and support for PHP.
For more articles about arrays and objects in js, please refer to PHP!