Array and dictionary usage of Javascript and property skills for Traversing objects _ javascript skills

Source: Internet
Author: User
Tags javascript array acer
The Array of Javascript is both an Array and a Dictionary ). Let's take an example to look at the Array usage of Javascript Array, which is both an Array and a Dictionary ). Let's take an example to see how to use arrays.

The Code is as follows:


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


Next, let's look at the usage of the dictionary.

The Code is as follows:


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


We can even traverse this array (dictionary) as above)

The Code is as follows:


For (var I in computer_price ){
Alert (I + ":" + computer_price [I]);
}


Here I is each key value of the dictionary. Output result:
Acer: 500
Dell: 1, 600

In addition, computer_price is a dictionary object, and each key value of computer_price is an attribute. That is to say, Acer is an attribute of computer_price. We can use it like this:
Computer_price.Acer

Let's take a look at the simplified declaration of dictionaries and arrays.
Var array = [1, 2, 3]; // array
Var array2 = {"Acer": 500, "Dell": 600}; // dictionary
Alert (array2.Acer); // 500
In this way, the dictionary statement is the same as the previous one. In our example, The Acer is a key value, but also an attribute of the dictionary object.

Next, let's take a look at how to traverse the attributes of an object. We can use for in to traverse object attributes.

The Code is 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, the Computer has two attributes: brand and price. Therefore, the output result is:
Computer [brand] = Acer
Computer [price] = 500
The preceding usage can be used to view the attributes of an object. When you know that the Computer object has a brand attribute, you can use
Mycomputer. brand
Or mycomputer [brand]
To obtain the property value.
Summary: arrays in Javascript can also be used as dictionaries. The dictionary key value is also the attribute of the dictionary object. You can use for in to repeat the properties of an object.

Array traversal and attributes
Although arrays in JavaScript are objects, there is no good reason to use for in to traverse arrays cyclically.
On the contrary, there are some good reasons not to use for in to traverse arrays.
Note: arrays in JavaScript are not associated arrays.
In JavaScript, only objects are used to manage key-value mappings. However, the associated array maintains the order, but the object is not.
Because the for in loop will enumerate all attributes on the prototype chain, the only way to filter these attributes is to use the hasOwnProperty function,
Therefore, it is much slower than the common for loop.
Traversal
To achieve the best performance of traversing arrays, we recommend that you use a classic for loop.

The Code is 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 preceding Code caches the length of an array through l = list. length.
Although length is an attribute of an array, accessing it in each loop still has performance overhead.
The latest JavaScript Engine may have been optimized at this point, but we cannot ensure that our code runs on these recent engines.
In fact, the method of not using the cached array length is much slower than that of the cached version.
Length attribute
The getter method of the length attribute simply returns the length of the array, while the setter method truncates the array.

The Code is as follows:


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


Note:
In Firebug, check that the foo value is: [1, 2, 3, undefined]
However, this result is not accurate. If you view the foo result on the Chrome console, you will find it like this: [1, 2, 3]
In JavaScript, undefined is a variable. Note that the variable is not a keyword, so the meaning of the above two results is completely different.
// Translator's note: for verification, we will execute the following code to check whether serial number 5 exists in foo.
5 in foo; // No matter in Firebug or Chrome, false is returned.
Foo [5] = undefined;
5 in foo; // true is returned no matter in Firebug or Chrome.
Setting a smaller value for length truncates the array, but increasing the value of length does not affect the array.
Conclusion
For better performance, we recommend that you use a common for loop and cache the length attribute of the array.
Using for in to traverse arrays is considered a bad code habit and tends to produce errors and cause performance problems.

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.