Tips for using Javascript Arrays-js tutorial

Source: Internet
Author: User
This article mainly introduces the use of Javascript arrays, including the array size, array traversal, and array methods, for more information about array indexes, see the previous article.

Array size

Js Arrays can be dynamically adjusted. More specifically, they do not have the concept of array out-of-bounds. a [a. length] is fine. For example, to declare an array a = [1, 3, 5], the current array size is 3, and the index of the last element is 2, but you can still use a [3], when accessing a [3], the returned result is undefined. assign a value to a [3]: a [3] = 7, which adds an element to array, now the length of array a is 4. You can try to put the following code in a browser and run it:

  var a = [];  for(int i = 0; i <= a.length; i++)  {    a[a.length] = i;  }

On my computer, Firefox will crash immediately, and chrome uses 99% of the cpu usage (viewed using chrome's task manager ).

The length Value of js changes with the change of array elements. Of course, you can also manually set the length attribute of the array. Setting a larger length will not allocate more space to the array, however, setting a smaller length will delete all attributes whose subscript is greater than or equal to the new length.

Another point is how the length value of the array comes from. It is correct to add one of the largest numeric index values, but if the empty slot is also counted, the length value is the number of elements in the array. The above figure explains:

From the values, we can see that an array a, a [0], and a [10] have been assigned values. At this time, the length of a is 11, there are nine empty slots in the middle (let's just translate them into empty slots ). The nine empty slots are not counted. I think we should calculate them so that we can reasonably explain the length value. What are the values of these empty slots? Undefined! Therefore, if you use foreach traversal (forin) in chrome, all empty slots can be skipped. If you use for traversal, undefined is printed. As for firefox, the performance is not the same. Try it yourself.

Array Traversal

When I was reading the js tutorial on Weibo yesterday, it said that when traversing the array, I would judge the statement

With regard to the foreach traversal of arrays, The js method is very strange compared to java/c # And Other languages:

For (var name in ['huey ', 'wey', 'louie']) {console. log (name);}/* print the result: 0 1 2 */

As you can see, the printed result is not an array element, but a digital index value (it seems that the js array is also stored in hash mode, note This. (As for why, I think array elements are all attributes of an array. This traversal is the traversal length value, from 0 to length. Instead of outputting the elements of the array one by one, because the elements are attributes and arrays are not only numeric indexes. Why do we only output them when traversing them like this, instead of length, push, join and other methods? To be fair, we have to output the numeric index of the array. Of course, this is just my opinion. I have not studied the specifics .)

Array Methods

The array has the push and pop methods, so the array is like a stack. To delete an array, you can remove an element from the array, but leave a hole in the array (that is, delete can also delete the elements in the array, but only delete the value of this position, does not change the array size. The original position type is undefined. This is because the elements after the deleted elements retain their original attributes, therefore, splice should be used to reduce the size of the array that has been deleted. It will remove the deleted attribute, but this is not very efficient. There are also map, reduce, filter and other methods in the array. Here we will not talk much about it (similar to list in python ).

Supplement

Finally, I have mentioned that arrays in js are objects (nonsense, originally objects). Can arrays and objects be replaced with each other? The answer is yes. However, in order to be clear, it is better to use them separately. The following describes when to use arrays and when to use objects (refer to the essence of javascript language):

If the attribute name is a small continuous integer, an array is used. Otherwise, an object is used.
In addition, because the results of using typeof for arrays and objects in js are all objects, the method to determine whether an Object is an array is:

  var is_array = function(value) {  return Object.prototype.toString.apply(value) === '[object Array]';  };

Fan Wai

I think the closure has been turned on. Maybe there are technical implementations at the language level, but at the application level, I think it should be like that. When I use it, I cannot feel that it is using the closure. However, this closure is almost a mandatory concept for the front-end of the interview.

The above is all the content of this article. I hope you will like it.

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.