JavaScript Advanced Programming Chapter fifth (1)

Source: Internet
Author: User
Tags access properties

A reference type is a data structure that combines data and functionality, similar to a class, but with different concepts: Although ECMAScript is an object-oriented language, it does not have the basic structure of classes and structures supported by traditional object-oriented languages. Reference types are also referred to as "object definitions."

I. Type of Object

Create Instance mode:

1 using the new operator + constructor

1 var New Object (); 2 person.name = "Lillian"; 3 person.age = 29;

2 Using object literals notation

2.1 Attribute without quotation marks

1 var person = {2person     : "Lillian",3     age:294 };

2.2 Attribute Quotes

1 var person = {2     ' person ': ' Lillian ',3     ' age ': 29 , 4     5:true  // numeric attributes are automatically converted to string 5 };

2.3 Using empty curly braces

1 var person = {}; 2 person.name = "Lillian"; 3 person.age = 29;

How to access properties:

Use point or square brackets:

1 alert (person.name); 2 alert (Person[age]);

Two, array type

How to create:

1 using the array constructor

1 var New Array (); 2 var New Array (a);  // length = 3 var New Array ("Red", "blue", "yellow");  // length = 3

2 Using array literals

1 var colors = ["Red", "blue", "yellow"]; 2 var names = []; 3 var values = [1, 2,]; // don't do this, you'll create an array with 2 or 3 items 4 var options = [,,,,,]; // don't do this, you'll create an array with 5 or 6 items

In addition to the object type, the array type is probably the most common type of JavaScript, it is a lot of methods, here first through the map list, followed by examples.

To modify the contents of array arrays:

1 varcolors = ["Red", "blue", "yellow"];2Colors[0] = "green";//Modify the first item3Colors[colors.length] = "BLACK";//add an entry with a value of "black"4 5Colors.length = 5; 6Alert (colors[4]);//The length is increased by 1, but is not assigned, this is undefined7 8Colors.length = 1;9Alert (colors[1]);//Undefined, removes all items except the first item by setting the length to 1

Detects that an object is not an array:

1 if instanceof Array) {}; // not available for NetEase with multiple frames 2 if // high requirement for browser version

Conversion method:

1 varcolors = ["Red", "blue", "yellow"];2Colors.tostring ();//" Red", "blue", "yellow"3Colors.tolocalestring ();//" Red", "blue", "yellow"4Colors.valueof ();//["Red", "blue", "yellow"]5alert (colors);//" Red", "blue", "yellow"6Alert (Colors.join (","));//" Red", "blue", "yellow"7Alert (Colors.join ("| |)");//"1| | 2| | 3 "

Stack method: LIFO

1 var colors = ["Red", "blue", "yellow"]; 2 Colors.push ("Black"); 3 colors.pop (); // "BLACK"

Queue method: LILO

1 var colors = ["Red", "blue", "yellow"]; 2 Colors.push ("Black"); 3 colors.shift (); // "Red"

Reorder methods:

1     varvalues = [1,0,15,14,2];2Values.reverse ();//[2, 0, 1]3Values.sort ();//[0, 1, +, 2]4 5     functionCompare (value1, value2) {6         if(Value1 <value2) {7             return-1;8}Else if(Value1 >value2) {9             return1;Ten}Else{ One             return0; A         } -     } -  theValues.sort (Compare);//0,1,2,14,15

Operation Method:

1     //concat () Add the received parameter at the end2     varcolors = ["Red", "green", "blue"];3     varColors2 = Colors.concat ("Yellow", ["Black", "Brown"]);4     //colors2: "Red", "green", "blue", "yellow", Black "," Brown "5 6     //Slice () intercepts part of the current array7     varcolors = ["Red", "green", "blue", "yellow"];8     varColors2 = Colors.slice (1);//["Green", "blue", "yellow"]9     varColors3 = Colors.slice (1,3);//["Green", "blue"]Ten  One     //Splice () Delete and insert A     varcolors = ["Red", "green", "blue", "yellow"]; -     varremoved = Colors.splice (0,2);//starting with the first item, delete two items -      the     varcolors = ["Red", "green", "blue", "yellow"]; -     varremoved = Colors.splice (1,0, "Black");//after the second item, insert an item -  -     varcolors = ["Red", "green", "blue", "yellow"]; +     varremoved = Colors.splice ("Black");//After the second item, delete an item, and then insert an item

Location Method:

1     var numbers = [1,2,3,4,5,4,3,2,1]; 2     // 3 3     // 5 4     // 5

Iterative methods:

1 /*2 Iteration Method: function (the value of the array item, the position of the item in the array, the array object itself)3 */ 4 5 //every (): If each item returns TRUE, the function returns True6 varnumbers = [1,2,3,4,5,4,3,2,1];7 varresult = Numbers.every (function(item, index, array) {8     return(Item > 2);9 }); Tenalert (result);//false One  A //Some (): If there is an item that returns TRUE, the function returns True - varnumbers = [1,2,3,4,5,4,3,2,1]; - varresult = Numbers.some (function(item, index, array) { the     return(Item > 2); - });  -alert (result);//true -  + //Filter (): Filters out values that meet the criteria - varnumbers = [1,2,3,4,5,4,3,2,1]; + varresult = Numbers.filter (function(item, index, array) { A     return(Item > 2); at });  -alert (result);//[3,4,5,4,3] -  - //map (): Returns the result of a function call and makes up an array - varnumbers = [1,2,3,4,5,4,3,2,1]; - varresult = Numbers.map (function(item, index, array) { in     return(Item * 2); - });  toalert (result);//[2,3,6,8,10,8,6,4,2]; +  - //ForEach (): No return value the varnumbers = [1,2,3,4,5,4,3,2,1]; *Numbers.foreach (function(item, index, array) { $ alert (item);Panax Notoginseng});

To reduce the method:

1 /*2 Iteration Functions: (previous value, current value, index of item, array object)3 iterate over all items and return a final result4 Reduceright is a forward traversal from the last item in the array5 */6 varvalues = [1,2,3,4,5];7 varsum = Values.reduce (function(prev, cur, index, array) {8 //var sum = values.reduceright (function (prev, cur, index, array) {9     returnPrev +cur;Ten }); Onealert (sum);// the

JavaScript Advanced Programming Chapter fifth (1)

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.