•Object type:
◦ Creation method:
/*new 操作符后面Object构造函数*/ var person = new Object(); person.name = "lpove"; person.age = 21; /*或者用对象字面量的方法*/ var person = { name: "lpove"; age : 21; }
•array type
◦ Create Method:
`var colors = new Array(“red”,”blue”,”yellow”);
• 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;
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!