JavaScript Advanced Programming-(4) reference types

Source: Internet
Author: User

1.object
The Var person={};//is the same as the new Object ()

General creation of objects

var person=new Object ();p Erson. Name= "admin";

JSON Way to create

var person={  Name: "admin",  age:23  };

Object is passed as a parameter

function Getperson (person p) {alert (p.name)}setperson ({Name: "admin", age:12});
2.Array

Array initialization:

var arr=new Array (1, "a", 3,4), Var arr2=[1, "A", 3,4];var arr3=[];

Check if array:

if (value instanceof Array) {//array operation}//or whether IsArray method if (Array.isarray (value)) {//array operation}

Array conversions

var people={"A",;p eople.tostring (),//a,34var colors=["Red", "Green"];colors.join (",");//red,green

Stack method

The stack is a last-in-first-out data structure, with the newest addition of the earliest removed

The push () method can receive any number of arguments, add to the end of the array, and return the latest array length

The Pop () method removes the last item from the end of the array, reducing the length value of the array

var colors=new Array (), Var count=colors.push ("Red", "green");  Insert 2 alert (count);   2count =colors.push ("Brown");  Insert another alert (count);   3var item = Colors.pop ();  Get the last alert (item);//Brownalert (colors.length);   2

Queue method

Shift () method to move the first item in the array to return the item, minus 1

Unshift () method, adding and returning the new array length at the front of the array

Example 1:

Example 2:

Sort by:

Reverse () and sort () method, reverse () implements array numeric inversion, sort sorted in order

Operation Method:

Concat () adds the received parameter to the end of the copy, returns the newly constructed array, and does not change the original array data

var colors=["Red", "green", "Blue"];var colors2=colors.concat ("Yellow", ["Black", "Brown"]), alert (colors);  Red,green,bluealert (COLORS2); Red,green,blue,yellow,black,brown

Slice () returns all items from the array that are specified as the primary start to the end of the current array, and does not affect the original array

var colors=["Red", "green", "blue", "yellow", "Purple"];var colors2=colors.slice (1); var colors3=colors.slice (1,4); alert (colors2);//green,blue,yellow,purplealert (COLORS3);//green,blue,yellow

Splice () Inserts an item into the middle of the array

Delete: You can delete any number of items by specifying only 2 parameters: the location of the first item to delete and the number of items to delete.
Insert: You can insert any number of items into the known location, providing only 3 parameters, starting position, 0 (number of items to delete), and items to insert
Replace: You can insert any number of items to the specified location and delete any number of items at the same time by specifying 3 parameters, starting position, the number of items to delete, and any number of items to insert. The number of items inserted does not have to be equal to the number of items deleted

var colors=["Red", "green", "Blue"];var removed=colors.splice (0,1); alert (colors);   Green,bluealert (removed);  Red, the returned array contains only one item of Removed=colors.splice (1,0, "Yellow", "orange");//Insert 2 alert (colors) from position 1;//green,yellow,orange, Bluealert (removed);//Returns an empty array Removed=colors.splice ("Red", "purple");//Insert 2 items, delete 1 alert (colors);//green,red, Purple,yellow,orange,bluealert (removed);//yellow, returns an array containing only one item

  

JavaScript Advanced Programming-(4) 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.