Array and object in js)

Source: Internet
Author: User
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!

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.