Instance properties (methods) for JavaScript arrays

Source: Internet
Author: User

All array instance objects of JavaScript, in addition to adding or removing attributes, inherit properties (methods) from Array.prototype. Modifying the prototype of an array affects all array instances.

Properties of an array instance:

Array.prototype.constructor: All arrays inherit this property, it's just ' array ', which indicates that all arrays are constructed with an array.

Array.prototype.length: Because Array.prototype is also an array, it also has the length property, which is 0 because it is an empty set. This is also not the length property of an array instance, and the array instance itself has the length property.

Properties of an Array instance (method):

1. Modifier methods: The following methods change the value of the object on which they are called:

Array.prototype.copyWithin (): Inside the array, copies a sequence of elements to the sequence of elements on the other end, overwriting the original value.

Array.prototype.fill (): Replaces the value of all elements of the specified interval in the array with a fixed value.

Array.prototype.pop (): Deletes the last element of the array and returns the element.

Array.prototype.push (): Adds one or more elements at the end of the array, and returns the new length of the array.

Array.prototype.reverse (): Reverses the order in which the elements in the array are arranged, that is, the first one becomes the last, and the last one becomes the first one.

Array.prototype.shift (): Deletes the first element of the array and returns the element.

Array.prototype.sort (): Sorts the arrays and returns the sorted array.

Array.prototype.splice (): Adds or removes any element to an array at any location.

Array.prototype.unshift (): Adds one or more elements at the beginning of the array and returns the new length of the array.

2, Access method: The following methods do not change the value of the array object, only return a new array or return a different expectation:

Array.prototype.concat (): Returns a new array composed of the current array and several other arrays or several non-array values.

Array.prototype.includes (): Determines whether the current array contains a specified value and returns False if true.

Array.prototype.join (): Concatenate all array elements into a string.

Array.prototype.slice (): Extracts an element from the current array into a new array.

Array.prototype.toSource (): Returns a String representing the current array literal. Object.prototype.toSource () is obscured on the prototype chain.

Array.prototype.toString (): Returns a string that consists of all array elements, and of course some other strings.

Array.prototype.toLocaleString (): Returns a localized string composed of all array elements.

Array.prototype.indexOf (): Returns the index of the first element in the array equal to the specified value, or 1 if no such element is found.

Array.prototype.lastIndexOf (): Returns the index of the last element in the array equal to the specified value, or 1 if no such element is found.

3, Iterative method: In the following many traversal methods, there are many methods need to specify a callback function as a parameter. The length property of an array is cached somewhere before each element is executed by the callback function.

So, if you add a new element to the current array in the callback function, then those newly added elements will not be traversed. In addition, if you do something else in the callback function for the current array, such as changing an element or deleting

An element, the subsequent traversal may be affected by an unintended effect. In summary, do not attempt to make any modifications to the original array during traversal, although the specification defines such operations in detail, but for readability and

Maintainability do not do so easily.

Instance properties (methods) for JavaScript arrays

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.