Experience in using JavaScript Arrays

Source: Internet
Author: User
Tags javascript array
Introduction to Webjx: This article describes how RickStrahl uses JavaScript arrays and introduces the differences between JavaScript processing arrays and other development languages, some may be things we take for granted, but they are not actually the case. This article introduces how to initialize a Web page using JavaScript arrays. This article describes how RickStrahl uses JavaScript arrays and how JavaScript processes arrays in some details, compared with other development languages, some of them may be something we take for granted, but they are not actually the case. It involves JavaScript Array initialization, length, and extended attributes.
  
This article is an example of RickStrahl's experience in using JavaScript arrays. It introduces the differences between JavaScript processing arrays and other development languages, some may be things we take for granted, but they are not actually the case. It involves JavaScript Array initialization, length, and extended attributes.
  
[Original] JavascriptArrayFun
  
【Author】 RickStrahl
  
Http://cuimingda.com/2009/01/javascript-array-fun.html.
  
[Translator] mingda
  
The following is a translation of the original text:
  
Josh left a comment in one of my previous posts, pointing out that some minor errors exist in CSSpropertywatchercomponent. This is a jQuery plug-in that stores CSS attributes in arrays, tracking the changes of these values. When the CSS property value changes, an event is thrown, and you can handle the event accordingly.
  
My error is how to define an array:
  
Vardata = {
  
Props: props. split (","),
  
Func: func,
  
Vals: [props. split (","). length],
  
ItvlId: itId
  
};
  
This Code creates an array of specific lengths, but the results are obviously not the same as I expected. In fact, this array only contains one element, which is the value of length. to initialize the array, the correct method should be as follows:
  
Vals: newArray (props. split (","). length)
  
But why is the plug-in running properly even if the method I previously defined is wrong? Because JavaScript can dynamically create an Array Based on the Value assignment without pre-initialization, it can run normally even if the definition method is incorrect.
  
$.Each(data.props,function(iction{data.vals? I =cancel.css (data. props [I]);});
  
This code traverses the props array and dynamically creates a new array by assigning values to the corresponding index of the vals array.
  
The JavaScript array can be automatically adjusted based on the assigned value, for example, the following example:
  
Varar = []
  
Ar [2] = 1;
  
Debugger;
  
The following result is displayed on the Firebug debugging interface. The array contains three elements.
  
Click to zoom in
  
The length of this array is 3, just like an initialized array. All array objects without assignment will be defined as undefined.
  
JavaScript can automatically re-adjust the size of the array, which is really great, but it may be possible to set a fixed size in advance, which can avoid frequent internal resizing of the array. Assigning values to an existing element is much faster than re-adjusting the size before each assignment.
  
This automatic resizing mechanism makes it easier to see out-of-bounds arrays (outofboundsarray) and to find out more. Pay more attention to similar situations.
  
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.