Reading Notes of javascript advanced programming (4) reference type and reference type

Source: Internet
Author: User

Reading Notes of javascript advanced programming (4) reference type and reference type

Chapter 5: reference types

Object Type

You can create an object instance in either of the following ways:

1. new Method

Var person = new Object ();

Person. name = "haozk ";

Person. age = 24;

2. Representation of object literal

Var person = {

Name: "haozk ",

Age: 24

}

You can also enclose attributes with strings:

Var person = {

"Name": "haozk ",

"Age": 24

}

There are two ways to access the attributes of an object instance:

1. alert (person ["name"]) // square brackets (attributes can be accessed through variables or keywords or reserved words)

2. alert (person. name) // Point Method

Array type

Unlike arrays in other programming languages, arrays in js have dynamic resizing of stringbuffer, and data item types can be different.

There are two ways to create, new and array literal:

Var colors = new Array ();

Var colors = new Array (3 );

Var colors = new Array ("red", "blue", "green ");

Var colors = [];

Var colors = ["red", "blue", "green"];

The length attribute of the array is not read-only and can be changed:

Colors. length = 2;

Alert (colors [2]); // undefined

Colors [colors. length] = 'black'; // Add a color in position 2. The current length is 3.

Detection array: (important)

Array. isArray () method

If (Array. isArray (value )){

// Operations on Arrays

}

Conversion Method:

To call the toLocaleString (), toString (), and valueOf () of the array, the returned results are strings separated by commas.

Alert () accepts string parameters and calls the toString () method in the background. Therefore, you can directly use alert (colors ).

To call the join () method, you can accept the parameter as a separator string, for example:

Alert (colors. join ["|"]) // red | blue | green

Stack method: push (), pop ()

Queue method: push (), shift (), unshift (can add any item at the front end of the array and return the length of the new array)

Re-sorting method:

Reverse: reverse ()

Ascending: sort () (Note: sort by string instead of numeric value)

Function compare (value1, value2) {// ascending (General)

If (value1 <value2 ){

Return-1; // in descending order, return 1;

} Else if (value1> value ){

Return 1; // in descending order, return-1;

} Else {

Return 0;

}

}

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

Values. sort (compare );

Alert (values );

Operation Method: contact (), slice (), splice ()

Location Method: indexOf (), lastIndexOf ()

Iteration Methods: every (), filter (), forEach (), map (), some ()

Reduction Method: reduce (), reduceRight ()

 

 

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.