A tentative study of JavaScript objects (iv)---Built-in objects array

Source: Internet
Author: User
Tags array length

We 're not going to tangle with God. Horse is built-in object, God Horse is built-in constructor. By then you will find that they are all objects.

Array () is a built-in constructor function that constructs an array:

var New

is equivalent to the following:

var arr = []; The method of array text table recognition

Whether the array is created in the form of a divine horse, we can add elements to it as usual.

Arr[0] = 1; arr[1] = 2//  [1, 2]

When we use the constructor function array () to create an array, we can also add elements to it by passing the value:

var New Array (All-in-all,true//

If we pass a single number to the constructor function, she will get an exception:

var New Array (5//  [undefinedX5]

Since the array was created by a constructor, she is not an object, the answer is YES! Yes that's an Object!!! We can verify that:

Console.log (typeof//  Object

Since it is an object, he inherits all the properties and methods of object.

For example: constructor, prototype and so on ....

Of course there are some unique features of arrays:

1. The property name of the array is incremented from 0 and the value is automatically generated.

2. The array has a length property that records the number of elements.

3. The array creates a number of additional built-in methods based on its parent object.

Let's verify the difference between an array and an object:

The Length property is automatically generated when the array is first defined, and the generic object has no Length property.

var a = [], B = {};a.length;   // 0b.length  //  undefined

Adding a numeric or non-numeric-keyed attribute to an array and an object is not a difference between the two:

A[0] = 1; b[0] = 1= 2= 2;

The Length property is updated with the number of number key names and ignores non-numeric key names.

We can also manually set the array length property, if the number is set greater than the number of elements in the array, the remainder will be filled with empty (undefined), but if the length property set is less than the number of elements of the current array then the element that is exceeded is automatically deleted.

the built-in method of the array:

Concat () Method:

The Concat () method is used to concatenate two or more arrays. The method does not alter the existing array , but only returns a copy of the concatenated array .

var a = [Ocument.write];d (a.concat (//  [1,2,3,4,5]
 var  arr = new  Array (3) arr[ 0] = "1" arr[ 1] = "2" arr[ 2] = "3" var  arr2 = new  Array (3) arr2[ 0] = "4" arr2[ 1] = "5" arr2[ 2] = "6" document.write (Arr.concat (arr2))  //   [1,2,3,4,5,6]  
var New Array (3) arr[0] = "1"arr[1] = "2"arr[2] = "3"varnew Array (3) arr2[ 0] = "4"arr2[1] = "5"arr2[2] = "6"varnew Array (2) arr3[0] = "7" c13>arr3[1] = "8"//  [1,2,3,4,5,6,7,8]

Join () Method:

The join () method is used to put all the elements in an array into a string. The element is delimited by the specified delimiter (the default is a comma).

var New Array (3) arr[0] = "1"arr[1] = "2"arr[2] = "3"//  ' the "
var New Array (3) arr[0] = "1"arr[1] = "2"arr[2] = "3"document.write (Arr.join ( // ' 1.2.3 '

Push () Method:

The push () method adds one or more elements to the end of the array and returns a new length (note that the length is longer, rather than returning a new array).

var New Array (3) arr[0] = "1"arr[1] = "2"arr[2] = "3"Console.log (Arr.push (///  4

Pop () Method:

The Pop () method is used to delete and return the last element of the array.

var New Array (3) arr[0] = "1a"arr[1] = "2a"arr[2] = "3a"//  3a

Reverse () Method:

The reverse () method reverses the order of the elements in the array.

var New Array (3) arr[0] = "1"arr[1] = "2"arr[2] = "3"//  [3,2,1] 

Unshift () Method:

The Unshift () method adds one or more elements to the beginning of the array and returns the new length .

var New Array () arr[0] = "1"arr[1] = "2"arr[2] = "3"Console.log (Arr.unshift (// [4,1,2,3]

Shift () Method:

The shift () method removes the first element from the array and returns the value of the first element.

var New Array (3) arr[0] = "1"arr[1] = "2"arr[2] = "3"//  1

Slice () Method:

The slice () method returns the selected element from an existing array.

Note that the method does not modify the array, but instead returns a subarray. If you want to delete an element from an array, you should use Method Array.splice ().

var New Array (3) arr[0] = "1"arr[1] = "2"arr[2] = "3"Console.log (Arr.slice (/// [2,3]
var New Array (6) arr[0] = "1"arr[1] = "2"arr[2] = "3"arr[3] = "4"arr[4] = "5"arr[5] = "6"Console.log (arr.slide (//  [3,4,5]

Splice () Method:

The splice () method adds/deletes an item to/from the array, and then returns the item that was deleted.

Note that the splice () method works differently than the slice () method, and the splice () method modifies the array directly.

var New Array (6) arr[0] = "1"arr[1] = "2"arr[2] = "3"arr[3] = "4"arr[4] = "5"arr [5] = "6"Console.log (Arr.split (2,0,111)); [1,2,111,4,5,6]
var New Array (6) arr[0] = "1"arr[1] = "2"arr[2] = "3"arr[3] = "4"arr[4] = "5"arr [5] = "6"Console.log (Arr.split (//[1,2,888,6]

Sort () Method:

The sort () method is used to sort elements of an array. A reference to an array. Note that the array is sorted on the original array, and no new arrays are generated.

var New Array (6) arr[0] = "Ten"arr[1] = "5"arr[2] = ""arr[3] = ""arr[4] = "1000" c7>arr[5] = "1"//  [1,10,1000,25,40,5]

The above code does not sort the numbers by the size of the numeric values, and to do this, you must use a sort function:

function Sortnumber (b) {  return A- b;} var New Array (6) arr[0] = "Ten"arr[1] = "5"arr[2] = ""arr[3] = ""arr[4] = "1000 "arr[5] =" 1 "//

A tentative study of JavaScript objects (iv)---Built-in objects array

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.