Talking about the use _javascript skill of javascript array

Source: Internet
Author: User
Tags hash javascript array

The previous article said the index of an array, which is the use of the array.

Size of the array

JS array can be dynamically resized, more specifically, it does not have the concept of array out of bounds, a[a.length] no problem. Like declaring an array a = [1, 3, 5], now the size of the array is 3, the last element of the index is 2, but you can still use a[3], Access a[3] return is undefined, give a[3] Assignment: a[3] = 7, is to add an element to array a, Now the length of array A is 4. You can try to put the following code in the browser run:

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

On my computer, Firefox will immediately collapse, and Chrome is a label that takes up 99% of the CPU (viewed using the Chrome Task Manager).

The value of the length of JS will change as the elements of the array change, of course, you can also manually set the length of the array, set a larger length does not give the array more space, but set a smaller length will cause all the subscript is more than equal to the new length of the property is deleted.

Another point is that the length of the array is how to come, and some data that is the largest number of numeric index plus one, should be right, but if the empty slot also counts, the length value is the number of elements of the array. The above picture explains:

As you can see from the diagram, there are arrays of a,a[0] and a[10] all assigned, at which time a is 11, with 9 empty slot in the middle (which translates into empty slots). Then these nine empty slots do not count, I think should be counted, so that you can reasonably explain the length of the value. What are the values of these empty slots? undefined! So, if you're using a foreach traversal (Forin) in Chrome, then these slots can just skip and use for traversal to print out undefined. As for Firefox, the performance is not the same, try it yourself.

Traversal of arrays

Yesterday in the micro-blog on the turn of the JS tutorial, which said in the traversal of the array, Judge statement i<a.length will cause each cycle to calculate the length of a time, so that a little impact on performance. I doubt that, I'm not sure that's the case, and different browsers on this situation may have different optimization, I searched the internet, found a few pages said to cache the size of the array, but are copied, no idea, also did not explain the principle, so I said very skeptical, and I was in the chrome test , there is no significant difference in performance. The point is, I think, length is the attribute of the array, every time you call A.length, just access this property, attributes are stored in a hash way, so the time complexity of Access is O (1). This is my opinion, if not, please let me know.

About the array of foreach traversal, JS Way compared to java/c# and other languages is very strange:

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

As you can see, the result of printing is not the elements of the array, but the numeric index value (it seems to be possible to say that the JS array is also stored in hash), however, this should be noted. (As for why, I think array elements are attributes of an array, which is the length value of the traversal, from 0 to length.) Instead of outputting the elements of an array individually, because the element is an attribute, and the array is not just a property of the numeric index, then why do you only output them in this way, rather than length,push,join? To be fair, you have to output the numeric index of the array. Of course, this is only my own opinion, specifically how I did not study. )

Some methods of array

Arrays have push and pop methods, so arrays are like stacks. The array uses the delete to remove an element from it, but that leaves a hole in the array (that is, delete deletes the element in the array, but only deletes the value of the position, does not change the size of the array, the original position type is undefined), This is because the elements that come after the deleted element retain their original attributes, so you should use splice to thin the array that has the delete operation, and it will remove the deleted property, but this is not very efficient. There are also maps, reduce, and filter methods 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, is the object), then is not to say that arrays and objects can be substituted for each other? The answer is yes. But in order to be clear, or separate use is better, the next when the use of the array, when the object (reference "JavaScript language pristine"):

When property names are small and contiguous integers, you should use an array, otherwise, use the object.
In addition, because the result of the array and object using TypeOf 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] ';
  };

Fan Wai

Think closures are deified, may be the language level of the implementation of technology, but in the application level I think it should be so ah, the use of the time is not felt that is the use of closures. But the closure has almost become the concept of the interview front end must ask.

The above mentioned is the entire content of this article, I hope you can enjoy.

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.