JavaScript's array and dictionary usage and traversal object's attribute skill _javascript skill

Source: Internet
Author: User
Tags acer

The array of Javascript arrays, both an array and a dictionary (Dictionary). Let's take a look at the use of arrays.

Copy Code code as follows:

var a = new Array ();
A[0] = "Acer";
A[1] = "Dell";
for (var i = 0; i < a.length; i++) {
Alert (A[i]);
}

Next look at the use of the dictionary.
Copy Code code as follows:

var computer_price = new Array ();
computer_price["Acer"] = 500;
computer_price["Dell" = 600;
Alert (computer_price["Acer"]);

We can even walk through the array like the above (the dictionary)
Copy Code code as follows:

for (var i in Computer_price) {
Alert (i + ":" + computer_price[i]);
}

Here I is the dictionary of each key value. The output results are:
acer:500
dell:600

In addition, Computer_price is a Dictionary object, and each key value of it is a property. That is to say that Acer is a computer_price attribute. We can use it this way:
Computer_price. Acer

Let's look at the simplified declaration of dictionaries and arrays.
var array = [1, 2, 3]; Array
var array2 = {"Acer": $, "Dell": 600}; Dictionary
Alert (array2. Acer); 500
The statement to the dictionary is the same as the previous one. In our example, Acer is a key value, but also as a property of the Dictionary object.

Let's take a look at how to iterate over the properties of an object. We can iterate over the properties of the object using for.
Copy Code code as follows:

function Computer (brand, price) {
This.brand = brand;
This.price = Price;
}
var mycomputer = new Computer ("Acer", 500);
For (Var prop in MyComputer) {
Alert ("computer[" + prop + "]=" + Mycomputer[prop]);
}

In the code above, computer has two properties, brand and price. So the output is:
Computer[brand]=acer
computer[price]=500
The above usage can be used to see what attributes an object has. When you already know that the computer object has a brand attribute, you can use the
Mycomputer.brand
or Mycomputer[brand]
To get the property value.
Summary: Arrays in JavaScript can also be used as dictionaries. The key value of the dictionary is also the property of the Dictionary object. When you iterate through the properties of an object, you can use for.

Array traversal and attributes
Although arrays are objects in JavaScript, there is no good reason to use a for in loop to traverse an array.
On the contrary, there are some good reasons not to use for-in traversal arrays.
Note: The array in JavaScript is not an associative array.
Only objects in JavaScript can manage the corresponding relationship of key values. But associative arrays are kept in order, and objects are not.
Because the for in loop enumerates all the properties on the prototype chain, the only way to filter these properties is by using the hasOwnProperty function,
So it will be many times slower than the normal for loop.
Traverse
In order to achieve the best performance of traversing arrays, the classic for loop is recommended.
Copy Code code as follows:

var list = [1, 2, 3, 4, 5, ... 100000000];
for (var i = 0, L = list.length i < l; i++) {
Console.log (List[i]);
}

The code above has a process of caching the length of an array through L = list.length.
Although length is an attribute of an array, there is a performance overhead in accessing it in each loop.
Perhaps the latest JavaScript engine has been optimized for this, but we can't guarantee that our code is running on these latest engines.
In fact, it is much slower than cached versions to not use the length of the cached array.
Length Property
The getter method of the length property simply returns the length of the array, and the setter method truncates the array.
Copy Code code as follows:

var foo = [1, 2, 3, 4, 5, 6];
Foo.length = 3;
Foo [1, 2, 3]
Foo.length = 6;
Foo [1, 2, 3]

Translator Note:
View in Firebug the value of Foo at this time is: [1, 2, 3, undefined, undefined, undefined]
But the results are not accurate, and if you look at the results of Foo on the Chrome console, you will find that: [1, 2, 3]
Because undefined is a variable in JavaScript, note that a variable is not a keyword, so the meaning of the above two results is completely different.
For verification, we'll execute the following code to see if ordinal 5 exists in Foo.
5 in Foo; return false either in Firebug or Chrome
FOO[5] = undefined;
5 in Foo; return true either in Firebug or Chrome
Setting a smaller value for length truncates the array, but increasing the length property value does not affect the pairs.
Conclusion
For better performance, it is recommended that you use a normal for loop and cache the length property of the array.
Traversing an array with a for in is considered a bad code habit and tends to generate errors and cause performance problems.

Related Article

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.