Three major property usages of array array in JS

Source: Internet
Author: User
Tags javascript array

Original: Three major attribute usages of array arrays in JS

Array arrays have 3 major properties, the length property, the prototype property, and the constructor property, respectively.

JS operation methods and properties of array arrays

This article summarizes the 3 properties of array arrays, the length property, the prototype property, the constructor property, and the note array Array object 8 classifications and several methods used, as follows:

3 Properties of an object

1. Length Property

Length Property

The Length property represents the size of the array, which is the number of elements in it. Because the index of an array always starts with 0, the upper and lower bounds of an array are: 0 and length-1. Unlike most other languages, the length property of a JavaScript array is variable, which requires special attention. When the length property is set larger, the state of the entire array does not change in fact, only the length property becomes larger, and when the length property is set to an earlier hour, the value of the element whose index is greater than or equal to length in the original array is lost. Here is an example that shows changing the length property:

    1. vararr=[12,23,5,3,25,98,76,54,56,76];
    2. //defines an array with 10 numbers  
    3. alert (arr.length);//Displays the length of the array  
    4. arr.length=12;//increase the length of the array  
    5. alert (arr.length);//The length of the display array has changed to  
    6. alert (arr[8]);//Displays the value of the 9th element, 56  
    7. arr.length=5;//reduce the length of the array to 5, and elements with an index equal to or more than 5 are discarded  
    8. alert (arr[8]);//display 9th element has changed to "undefined"  
    9. arr.length=10;//to restore the array length to ten  

By the above code we can clearly see the property of the length property. But the length object can be not only explicitly set, it may also be implicitly modified. You can use an undeclared variable in JavaScript, or you can use an undefined array element (an element that has an index that exceeds or equals length), at which point the value of the length property is set to the value of the index of the element being used plus 1. For example, the following code:

    1. VARARR=[12,23,5,3,25,98,76,54,56,76];
    2. Defines an array that contains 10 numbers
    3. alert (arr.length);//Display 10
    4. arr[15]=34;
    5. alert (arr.length);//Display 16

The code also defines a 10-digit array, with the alert statement to see its length as 10. An element with an index of 15 is then assigned a value of 15, or arr[15]=34, and then the length of the array is output with the alert statement, resulting in 16. In any case, this is a surprising feature for developers who are accustomed to strongly typed programming. In fact, an array created using the form NewArray (), whose initial length is 0, is the operation of an undefined element in it, which changes the length of the array.

As you can see from the above introduction, the length property is so magical that it makes it easy to increase or decrease the capacity of the array. Therefore, an in-depth understanding of the length attribute can be used flexibly in the development process.

2. Prototype Properties

Prototype property

Returns a reference to the object type prototype. The prototype property is common to object.

Objectname.prototype

The objectname parameter is the name of the object.

Description: Provides a basic set of functions for a class of objects using the prototype property. The new instance of the object "inherits" the action given to the object's prototype.

For arrays array objects, use the following example to illustrate the purpose of the prototype property.

Adds a method to the array object that returns the value of the largest element in the array. To do this, declare a function, add it to array.prototype, and use it.

  1. Functionarray_max ()
  2. {
  3. Vari,max=this[0];
  4. For (i=1;i<this.length;i++)
  5. {
  6. if (max<this[i])
  7. max=This[i];
  8. }
  9. Returnmax;
  10. }
  11. array.prototype.max=Array_max;
  12. varx=NewArray (1,2,3,4,5,6);
  13. vary=X.max ();

After the code executes, Y saves the maximum value in the array x, or 6.

3. Constructor properties

Constructor property

Represents a function that creates an object.

Object.constructor//object is the name of the object or function.

Description: The constructor property is a member of all objects that have prototype. They include all JScript intrinsic objects except the global and math objects. The constructor property holds a reference to a function that constructs a particular object instance.

For example:

    1. x=newstring ("Hi");
    2. if (x.constructor==string)//For processing (condition is true).
    3. Or
    4. functionmyfunc{
    5. function body.
    6. }
    7. y=Newmyfunc;
    8. if (y.constructor==myfunc)//For processing (condition is true).

For arrays:

    1. y=NewArray ();

8 categories of array objects and multiple methods

Creation of 1.Array arrays

Vararrayobj=newarray (); Create a default array with a length of 0
Vararrayobj=newarray (size); Create an array of size length, note that the length of the array is variable, so it is not the upper bound, is the length
Vararrayobj=newarray (item1,item2,); Create an array and assign an initial value
To illustrate, although the second method creates an array that specifies the length, the array is actually variable in all cases, meaning that even if the length is 5, the element can still be stored outside the specified length, note that the length changes accordingly.

2. Access to elements of array arrays

vararrayitemvalue=arrayobj[1];//gets the element value of the array
Arrayobj[1]= "To give new value";//Assign a new value to an array element

Three major property usages of array array in JS

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.