On the use of JavaScript arrays

Source: Internet
Author: User

The size of the array

The JS array can be resized dynamically, more specifically, without the concept of an array out of bounds, a[a.length] nothing wrong. For example, declare an array a = [1, 3, 5], now the array size is 3, the last element index is 2, but you can still use a[3], Access a[3] return is undefined, to a[3] Assignment: a[3] = 7, is to add an element to the array a , the length of array A is now 4. You can try to put the following code in the browser to run:

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

  

On my computer, Firefox will instantly pop up, Chrome, a label that consumes 99% of the CPU (viewed with Chrome's task Manager).

The value of the length of JS will change as the array elements change, of course you can also manually set the length property of the array, setting a larger length does not allocate more space to the array, However, setting a smaller length causes all attributes with a subscript greater than or equal to the new length to be deleted.

Another point is that the length value of the array is how to come, some data that is the largest one numeric index value plus one, should be right, but if the empty slot is also counted, the length value is the number of elements of the array.

As can be seen from the figure, there are arrays of a,a[0] and a[10] have been assigned, this time a of length is 11, in the middle there are 9 empty slots (let's translate to empty slot good). The nine empty slots do not count, I think it should be counted, so that can reasonably explain the length value. What are the values of these empty slots? undefined! So, if you're using a foreach traversal (Forin) in Chrome, then these slots are all going to be skipped, and a for traversal will print out undefined. As for Firefox, the performance is not the same, try it yourself.

Traversal of an array

Yesterday in the micro-blog on the turn of the JS tutorial, it said in the traversal of the array, the judgment statement i<a.length will cause each cycle to be calculated once the length, which has a little impact on performance. This point I doubt, I am not sure whether this is the case, and different browsers may have different optimizations for this situation, I searched the internet, found a few pages said to cache the array size, but are copied, no ideas, also did not explain the principle, so I am very skeptical, and I tested in the chrome , there is no significant difference in performance. The key is I think, length is the property of the array, each time I call a.length, just access this property, the property is stored in hash way, so the time complexity of Access is O (1). This is my opinion, if not, please tell me.

With respect to the foreach traversal of arrays, JS is a strange way to java/c# languages like:

for (var name in [' Huey ', ' Dewey ', ' Louie ')} {  Console.log (name);}  /* Printed results:  0  1  2*/

  

As you can see, the result of printing is not an array of elements, but a numeric index value (it seems as if it can also be explained that theJS array is also stored in hash), regardless, this point should be noted. (As for why, I think the array element is the property of the array, and this traversal is the length value of the traversal, from 0 to length.) Instead of outputting the elements of the array one by one, because the element is an attribute, and the array is not just a property of the numeric index, then why do you just output them when you traverse them, rather than length,push,join? To be fair, we had to output the numeric index of the array. Of course, this is just my own opinion, specifically how I did not study. )

Some methods of arrays

Arrays have push and pop methods, so arrays are like stacks. Using delete on an array, you can remove an element from the arrays, but that leaves an empty hole in the array (that is, delete can also delete the elements in the array, but only the value of that position is deleted, the array size is not changed, the original position type is undefined), This is because the elements that follow the deleted elements retain their original properties, so you should use splice to thin the array of delete operations, which will remove the deleted attributes, but this is not very efficient. There are maps, reduce, filter, and so on in the array, not much to say (like the list in Python).

Add

Finally add, I said earlier, JS in the array is the object (nonsense, it is the object), then it is not said, arrays and objects can be used to replace each other? The answer is yes. But in order to clear, or separate with the better, the following is the next time to use an array, when to use the object (refer to the "JavaScript Language essence"):

An array should be used when the property name is a small, contiguous integer, otherwise, the object is used.
In addition, because the result of using typeof in the array and object in JS is object, the method of determining whether an object is an array is:

var Is_array = function (value) {return Object.prototype.toString.apply (value) = = = ' [Object array] ';};

  


Pan Wai

Think the closure is deified, may be the language level of the implementation of technology, but at the application level I think it should be like that ah, when using it is not to feel that is the use of closures. But this closure is almost a must-ask concept in front of the interview.

The above mentioned is the whole content of this article, I hope you can like.

On the use of JavaScript arrays

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.