JS Object-Oriented Learning note three (Array object)

Source: Internet
Author: User

1. Initialization of the array:
Three initialization modes new array (); new array (size); new Array (element0, element1, ..., ELEMENTN);

2. Properties
Constructor     returns a reference to the array function that created this object. Length             Sets or returns the number of elements in the array. Prototype       gives you the ability to add properties and methods to objects.


3. Methods
Concat () joins two or more arrays and returns the result.
Use:
Arrayobj.concat (Aobj1,aobj2 ...);


Join () Places all elements of an array into a string. element is delimited by the specified delimiter.
ToString () converts the array to a string and returns the result.
ValueOf () returns the original value of the array object
Use:
Arr.join (".")           Arr.tostring ()//default with comma ', ' split



Pop () deletes and returns the last element of the array
Push () adds one or more elements to the end of the array and returns the new length.
Use:
Arrobj.pop ();      Arrobj.push ("theme");



Shift () deletes and returns the first element of the array
Unshift () adds one or more elements to the beginning of the array and returns the new length.
Use:
Arrobj.shift ()     arrobj.unshift ("Element1")


Reverse () Reverses the order of the elements in the array.
Use:
Arrobj.reverse ()

Tosource () returns the source code of the object.
Arrobj.tosource (); ["George", "John", "Thomas"]


Sort () Sorts the elements of an array
Use:
var ArrA = [6,2,4,3,5,1];    function desc (x, y) {          if (× >                return-1);          if (x < y)                        return 1;    }    function asc (x, y) {          if (× > y)                return 1;          if (x < y)                        return-1;   } Alert (Arra.sort (ASC))  //Ascending alert (Arra.sort (DESC))//descending order



Slice () returns the selected element from an existing array
Use:
Arra.slice (2,4)  //Returns an element with an index of 2 to 4 [2,4] before closing                                        //4,3


Splice () Deletes the element and adds a new element to the array. Returns the deleted item

Use:
Arrayobject.splice (index,howmany,item1,....., ItemX)

Index required. An integer that specifies the location of the Add/remove item, using a negative number to specify the position from the end of the array.
Howmany required. The number of items to delete. If set to 0, the item is not deleted.

Item1, ..., ItemX Optional. Adds a new item to the array.



JS Object-Oriented Learning note three (Array object)

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.