JavaScript reference types

Source: Internet
Author: User

1 Object Type

var person = new Object (); Person.name = "Summer"; and var person = {name: ' Summer '}; is equivalent.

2 Array Type

(1) var colors = new Array (); and var colors = Array (); and var colors = []; is equivalent.

(2) The Length property in an array is not only read-only, that is, by setting this property, you can remove an item from the end of the array or add a new item.

Example 1:

var colors = [' Red ', ' blue ', ' green '];

Colors.length = 2;

Alert (colors[2]); Undefined

Example 2:

var colors = [' Red ', ' blue ', ' green '];

Colors.length = 4;

Alert (colors[3]); Undefined, but the array has been added
Colors[colors.length] = ' black ';//array plus one

Example 3:

var colors = [' Red ', ' blue ', ' green '];

COLORS[99] = ' black ';

alert (colors.length); 100

(3) Array of detections: value instanceof Array and Array.isarray (value)

(4) Loading method: Tolocalstring (), toString (), ValueOf (), and join

Example 1:

var colors = [' Red ', ' blue ', ' green '];

Alert (Colors.join ("| |)"); red| | green| | Blue

(5) Stack method (LIFO) LIFO: Colors.push ("Red", "green" ...) (added in the back) and Colors.pop () (removed from the back)

(6) Queue method (FIFO) FIFO: Colors.unshift ("Red", "green" ...) (add in front) and Colors.unshift () (delete from front)

(7) Reflow method: Reverse () and sort ()

Example 1:

var values = [1,2,3,4,5] '

Values.reverse ();

alert (values);//5,4,3,2,1

Example 2:

var values = [0,1,5,10,15] '

Values.sort ();

alert (values);//0,1,10,15,5 because sort does not pass parameters, it is arranged by string

Example 2:

function Compare (value1,value2) {

if (value1 < value2) {

return-1;

}else if (value1 > value2) {

return 1;

}else{

return 0;

}

}

var values = [0,1,5,10,15];

Values.sort (Compare);//Incoming function parameter

alert (values);//0,1,5,10,15

(8) Operation method: Concat (), slice (), splice ()

Concat (): Creates a new array based on all items in the current array

Example 1:

var colors = [' red ', ' green ', ' Blue '];

var colors2 = colors.concat (' Yellow ', [' black ', ' brown ']);

alert (colors);//red,green,blue

alert (colors2);//red,green,blue,yellow,black,brown

Slice (): Creates a new array based on one or more items in the current array

Example 2:

var colors = [' red ', ' green ', ' blue ', ' yellow ', ' purple '];

var colors2 = Colors.slice (1);//A parameter, starting from 1 to the end

var colors3 = Colors.slice (1,4);//two parameters, from 1 to 4, excluding end items

alert (colors2);//green,blue,yellow,purple

alert (COLORS3);//green,blue,yellow

Splice (): Delete, insert, replace (the splice () method always returns an array that contains the items removed from the original array, and an empty array if no items are deleted)

Delete: You can delete any number of items by specifying only 2 parameters: the position of the first item to be deleted and the number of items to be deleted, such as splice (0,2), removes the first two items in the array

Insert: You can insert any number of items to the specified location, providing only 3 parameters: Start position, 0 (number of items to delete), and items to insert. such as splice (2,0, "Red", "green", "yellow" ...) Inserts the following string starting at position 2 of the current array

Replace: You can insert any number into the specified location and delete any number of items at the same time by specifying 3 parameters: The starting position, the number of items to delete, and the items for any data you want to insert. The number of items inserted does not have to be equal to the number of items deleted. such as splice (2,1, "Red", "green" ...)

Example 3:

var colors = [' red ', ' green ', ' Blue '];

var removed= colors.splice (0,1);

alert (colors);//green,blue

alert (removed);//red

Removed= Colors.splice (1,0, "Yellow", "orange");

alert (colors);//green,blue,yellow,orange

alert (removed);//null

removed= Colors.splice ("Red", "purple");

alert (colors);//green,red,purple,orange,blue

alert (removed);//null

JavaScript reference types

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.