JS Reference type

Source: Internet
Author: User

1. Type of Object

How objects are created

1) constructor mode var obj = new Object (); Object is a constructor

2) literal way var obj = {}

2. Array type

Each item of the ECMAScript array can hold any type of data

How arrays are created

1) var color = new Array ();

If you know the number of items in the array to be saved beforehand, you can pass the number of changes to the constructor, which automatically becomes the value of the length property.

var arr = new Array (20);

You can also pass the items that should be included in the array to the constructor

var arr = new Array (' xhk ', ' Coco ', ' lover ');

If you pass a value to the constructor

This value is a number that creates an array that contains the given number of items.

This value is another type parameter that creates an array containing only one item of this value

You can omit the new operator when using the array constructor

var arr = Array (3);

2) array literal var arr = [];

detecting arrays

instanceof Array

Stack method (LIFO)

Push (): Accepts any number of arguments, adds them to the end of the array one by one, and returns the length of the modified array.

Pop (): Removes the last item from the end of the array, reduces the length of the array, and returns the item that was removed.

Queue Method (FIFO)

The queue adds items at the end of the list, removing items from the front end of the list.

Shift (): Removes an item from the front of the array and returns the item.

Unshift (): Adds any item in the front of the array and returns the length of the new array.

  

Reorder method: Reverse () sort ()

Reverse (): Reverses the order of the array items, returning the reordered array.

Sort (): By default, array items are sorted in ascending order, small to large, and the sort () method invokes the ToString () transformation method for each array item, and then compares the string to determine how to sort, even if each item in the array is a numeric value, the sort () method compares the string. Returns the reordered array.

var arr = [0, 1, 5, 15, 20]; [0, 1, 15, 20, 5]

Operation method

Concat (): Creates a copy of the current array, appends the received parameters to the end of the copy, and returns the newly constructed array. If one or more arrays are passed to the concat () method, the method adds each item in the array to the result array. The original array does not change.

Slice (): Creates a new array based on one or more items of the current array. Accepts one or two parameters, parameter one: Returns the starting position of the item, and two parameters: Returns the end position of the item;

does not affect the original array

If there is only one argument, all items from the specified position of the parameter to the end of the array are returned.

If two parameters, returns the entry between the start and end positions, not the item that contains the end position.

If one of the parameters of the slice () method is a negative number, then the array length is used to determine the corresponding position, eg:arr.length = 5; Slice (-2,-1); Should actually return the new array of slice (3, 4);

Splice (): Inserts an item into the middle of the array. Returns the item containing the deletion from the original array. If no items are deleted, an empty array is returned.

1) Delete: Delete any number of items: Return the deleted item, two parameters, parameter one, the position of the first item to delete, parameter two, the number of items to delete.

2) Insert: Insert any number of items at the specified position, parameter one, start position, parameter two, 0 (number of items to delete), parameter three, item to insert

3) Replace: Insert any number of items to the specified location, and delete any number of items at the same time. Parameter one, starting position, parameter two, number of items to delete, parameter three, any number of items to be inserted.

Location method

IndexOf (): parameter: Parameter one, the item to find, parameter two, (optional) an index to find the starting point, and a lookup from the beginning of two of the array, returning the position of the found item in the array

LastIndexOf (): parameter: Parameter one, the item to find, parameter two, (optional) the index at which to find the starting point, and a forward lookup from the end of the array, returning the position of the found item in the array

Iterative methods

Each method accepts two parameters, parameter one, the function that runs on each item, parameter two, (optional), the scope object that runs the function--the value that affects this

Every (): Each item in an array runs the given function, and returns True if every item of the function returns true;

Filter (): Each item in an array runs the given function, returning the function to return true to the arrays of items, and querying all array items that meet certain criteria is useful.

ForEach (): Runs the given function for each item in the array, with no return value.
Map (): Each item in the array runs the given function, returning an array that consists of the results of each function call. Suitable for creating an array containing the items corresponding to another array of one by one.

Some (): Runs the given function for each item in the array, and returns True if the function returns true for either item.

JS Reference type

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.