Method and property instance resolution of JS operation Array Array

  • 2020-03-30 01:14:41
  • OfStack

This article summarizes the three attributes of Array Array, including the use of length attribute, prototype attribute and constructor attribute, and notes the use of eight categories and multiple methods of Array objects, as follows:

Three properties of the object
1. Length attribute

Length attribute
The Length attribute represents the Length of the array, which is the number of elements in it. Since the index of an array always starts at 0, the bounds of an array are 0 and length-1, respectively. Unlike most others, the length property of a JavaScript array is mutable, which is something to be careful about. When the length property is set larger, the state of the entire array does not actually change, just the length property. When the length attribute is set smaller than the original value, the values of elements in the original array whose index is greater than or equal to the length are all lost. Here is an example of changing the length property:

Var arr =,23,5,3,25,98,76,54,56,76 [12]; // defines an array of 10 Numbers
Alert (arr. Length); // shows the length of the array 10
Arr. Length = 12; // increases the length of the array
Alert (arr. Length); // shows that the length of the array has changed to 12
Alert (arr [8]); // shows the value of the ninth element, which is 56
Arr. Length = 5; // reduces the length of the array to 5, and the elements with an index equal to or greater than 5 are discarded
Alert (arr [8]); // shows that the ninth element has been changed to "undefined"
Arr. Length = 10; // restores the array length to 10
Alert (arr [8]); // although the length has been restored to 10, the 9th element cannot be retrieved, showing "undefined"

From the above code we can clearly see the nature of the length property. But the length object can not only be set explicitly, it can also be modified implicitly. JavaScript can use an undeclared variable, as well as an undefined array element (an element whose index is greater than or equal to length), where the value of the length attribute is set to the value of the index of the element being used plus 1. For example, the following code:

Var arr =,23,5,3,25,98,76,54,56,76 [12]; // defines an array of 10 Numbers
Alert (arr. Length); / / display 10
Arr [15] = 34;
Alert (arr. Length); / / display 16

The code also defines an array of 10 Numbers, whose length is 10 through the alert statement. We then used an element with an index of 15, assigned it to 15, that is, arr[15]=34, and then printed the length of the array with an alert statement, which gives us 16. In any case, this is a surprising feature for developers used to strongly typed programming. In fact, the initial length of an Array created in the form of new Array() is 0, and it is the operation on the undefined elements that changes the length of the Array.

As you can see from the above, the length attribute is so magical that it can be easily used to increase or decrease the size of an array. Therefore, a deep understanding of the length attribute can be used flexibly in the development process.

2. Prototype property

The prototype property
Returns a reference to an object type stereotype. The prototype property is Shared with object.

The objectName. Prototype

The objectName parameter is the name of the object object.

Note: the prototype property provides a set of basic functionality of the object's class. A new instance of an object "inherits" the operations assigned to the object stereotype.

For array objects, the prototype property is used as an example.

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

The function array_max ()
{
    Var I, Max = this[0];
    For (I = 1; I < This. Length; I++)
    {
            If (Max < This [I])
            Max = this [I];
    }
    The return of Max;
}

Array. The prototype. Max = array_max;
Var x = new Array(1, 2, 3, 4, 5, 6);
Var y is equal to x.ax ();

After this code is executed, y holds the maximum value in the array x, or 6.

3. Constructor attribute

The constructor property
Represents a function that creates an object.

Constructor //object is the name of an object or function.

The constructor property is a member of any object with a prototype. They include all JScript native objects except Global and Math objects. The constructor attribute holds a reference to a function that constructs a specific object instance.

Such as:

X = new String (" Hi ");
If (x.c structor == String) // handle (condition true).
/ / or
The function MyFunc {
// the body of the function.
}

Y = new MyFunc;
If (y.constructor == MyFunc) // handle (condition true).

For arrays:
Y = new Array ();

Array objects with 8 categories and multiple methods

1. Create an array

Var arrayObj = new Array(); // creates a default array of length 0
Var arrayObj = new Array(size); // create an Array of size length, note that the length of the Array is variable, so it is not the upper limit, but the length
Var arrayObj = new Array(item1,item2,); // creates an array and assigns an initial value
Note that while the second method of creating an array specifies a length, in all cases the array actually gets longer, which means that even if you specify a length of 5, you can still store the element outside the specified length. Note that the length changes.

2. Access to the elements of the array

Var ArrayItemValue = arrayObj [1]. // gets the element value of the array
ArrayObj [1]= "to assign a new value "; // assigns a new value to an array element

This article summarizes the three attributes of Array Array, including the use of length attribute, prototype attribute and constructor attribute, and notes the use of eight categories and multiple methods of Array objects, as follows:

Three properties of the object
1. Length attribute

Length attribute
The Length attribute represents the Length of the array, which is the number of elements in it. Since the index of an array always starts at 0, the bounds of an array are 0 and length-1, respectively. Unlike most other languages, JavaScript arrays have a variable length property, which is something to be careful about. When the length property is set larger, the state of the entire array does not actually change, just the length property. When the length attribute is set smaller than the original value, the values of elements in the original array whose index is greater than or equal to the length are all lost. Here is an example of changing the length property:

Var arr =,23,5,3,25,98,76,54,56,76 [12]; // defines an array of 10 Numbers
Alert (arr. Length); // shows the length of the array 10
Arr. Length = 12; // increases the length of the array
Alert (arr. Length); // shows that the length of the array has changed to 12
Alert (arr [8]); // shows the value of the ninth element, which is 56
Arr. Length = 5; // reduces the length of the array to 5, and the elements with an index equal to or greater than 5 are discarded
Alert (arr [8]); // shows that the ninth element has been changed to "undefined"
Arr. Length = 10; // restores the array length to 10
Alert (arr [8]); // although the length has been restored to 10, the 9th element cannot be retrieved, showing "undefined"

From the above code we can clearly see the nature of the length property. But the length object can not only be set explicitly, it can also be modified implicitly. JavaScript can use an undeclared variable, as well as an undefined array element (an element whose index is greater than or equal to length), where the value of the length attribute is set to the value of the index of the element being used plus 1. For example, the following code:

Var arr =,23,5,3,25,98,76,54,56,76 [12]; // defines an array of 10 Numbers
Alert (arr. Length); / / display 10
Arr [15] = 34;
Alert (arr. Length); / / display 16

The code also defines an array of 10 Numbers, whose length is 10 through the alert statement. We then used an element with an index of 15, assigned it to 15, that is, arr[15]=34, and then printed the length of the array with an alert statement, which gives us 16. In any case, this is a surprising feature for developers used to strongly typed programming. In fact, the initial length of an Array created in the form of new Array() is 0, and it is the operation on the undefined elements that changes the length of the Array.

As you can see from the above, the length attribute is so magical that it can be easily used to increase or decrease the size of an array. Therefore, a deep understanding of the length attribute can be used flexibly in the development process.


2. Prototype property

The prototype property
Returns a reference to an object type stereotype. The prototype property is Shared with object.

The objectName. Prototype

The objectName parameter is the name of the object object.

Note: the prototype property provides a set of basic functionality of the object's class. A new instance of an object "inherits" the operations assigned to the object stereotype.

For array objects, the prototype property is used as an example.

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

The function array_max ()
{
    Var I, Max = this[0];
    For (I = 1; I < This. Length; I++)
    {
            If (Max < This [I])
            Max = this [I];
    }
    The return of Max;
}

Array. The prototype. Max = array_max;
Var x = new Array(1, 2, 3, 4, 5, 6);
Var y is equal to x.ax ();

After this code is executed, y holds the maximum value in the array x, or 6.

3. Constructor attribute

The constructor property
Represents a function that creates an object.

Constructor //object is the name of an object or function.

The constructor property is a member of any object with a prototype. They include all JScript native objects except Global and Math objects. The constructor attribute holds a reference to a function that constructs a specific object instance.

Such as:

X = new String (" Hi ");
If (x.c structor == String) // handle (condition true).
/ / or
The function MyFunc {
// the body of the function.
}

Y = new MyFunc;
If (y.constructor == MyFunc) // handle (condition true).

For arrays:
Y = new Array ();

Array objects with 8 categories and multiple methods

1. Create an array
Var arrayObj = new Array(); // creates a default array of length 0
Var arrayObj = new Array(size); // create an Array of size length, note that the length of the Array is variable, so it is not the upper limit, but the length
Var arrayObj = new Array(item1,item2,); // creates an array and assigns an initial value

Note that while the second method of creating an array specifies a length, in all cases the array actually gets longer, which means that even if you specify a length of 5, you can still store the element outside the specified length. Note that the length changes.

2. Access to the elements of the array
Var ArrayItemValue = arrayObj [1]. // gets the element value of the array
ArrayObj [1]= "to assign a new value "; // assigns a new value to an array element


Related articles: