"JavaScript advanced Programming"--Reference type Object, Array

Source: Internet
Author: User
Tags prev
Object

1. There are two ways to create an object instance:

var person=new Object ();
Person.name= "ZX";
person.age=19;

Object literal notation:

var person={
    Name: "ZX",
    age:19
};

Live in the middle of each of the two attributes separated by commas, the last attribute after the comma, the end of a semicolon.

2. When accessing object properties, you can use point notation or square brackets notation. When using the latter, be aware that the attribute should be enclosed in quotation marks.
The advantage of using square brackets notation is that you can access the property through a variable, or the property name contains characters such as spaces. Array

Each item of the ECMAScript array can hold any type of data, and its size is dynamically adjusted.

Several construction methods:

var arr=new Array ();
var arr=new Array (a);
var arr=new Array ("Red", "black" and "white");
Array literal notation
var arr=[];
var arr=["Red", "black" and "white";

You can get the current length of the array through the Length property:

var arr=["Red", "black" and "white";
Arr[arr.length]= "Pink";
arr.length=2;
Alert (arr[2])//undefined
arr[99]=19;
alert (arr.length);//100


1. Detection array

The problem with using instanceof is that it assumes that there is only one global environment. You can detect an array in the following ways:

if (Array.isarray (value)) {
   dosomething ();
}
2. Conversion methods

ToString (), tolocalestring (), valueof ().
Calling the array's ToString method returns a comma-delimited string of strings of each value in the array, and this method actually invokes the ToString method for each item. Calling the toLocaleString method is similar, except that it invokes the toLocaleString method for each item. Call the ValueOf method returns an array.

var arr=["Red", "black" and "white";
Alert (arr.tostring ());//"Red,black,white"

Join () can accept an argument as a separator:

var arr=["Red", "black" and "white";
Alert (Arr.join ("&"));//"Red&blck&white"
3. Stack method

Push () can accept any number of arguments, add them to the end of the array, and return the length of the array.
Pop () moves the last item of the divisor group and returns the removed item.

var arr=["Red"];
Arr.push ("Black" and "white");
alert (arr.length);//3
var item=arr.pop ();
alert (item);//"White"
4. Queue Method

Shift () is able to move the first item in the array and return it.
Unshift () can add any item to the front of the array and return the length.
Combine the use of shift () and push () to simulate queues;
Combined with Unshift () and Pop (), you can simulate a reverse queue.

var arr=["Red"];
Alert (Arr.unshift ("Black", "white"));//"black,white,red"
alert (Arr.shift ());//"white,red"
5. Reordering Methods

One is reverse ().

var arr=[1,2,3,4,5];
Arr.reverse ();
Alert (arr);//[5,4,3,2,1]

The other is sort (). It accepts a comparison function to specify the collation.
The comparison function accepts two arguments, if the first argument returns a negative number before the second argument, or 0 if it is equal, and returns a positive number if the first argument should be after the second argument:

function Compare (value1,value2) {
    if (value1<value2) {
        return-1;
    } else if (value1==value2) {return
        0;
    } else return
        1;
}

var arr=[1,9,2,7,3];
Arr.sort (Compare);

In the case of a numeric array, you can also simplify:

function Compare (value1,value2) {return
    (value1-value2);
}
6. Method of Operation

The first one is concat ().

var arr=["Red"];
Arr.concat ("Black" and "white");
Alert (arr);//["Red", "black" and "white"

The second one is slice (). When a parameter is accepted, all items in the array that start from the item to the end are returned, and when two parameters are accepted, all items in the array, but not the end position, are returned. The method does not affect the original array.

var arr=[1,2,3,4,5,6];
Alert (Arr.slice (2));//[3,4,5,6]
alert (Arr.slice (2,4));//[3,4]

The third one is splice (). When two parameters are accepted, the first parameter represents the starting position and the second parameter represents the number to delete. When more than two parameters are accepted, the first represents the starting position, the second represents the number to delete, and the following represents the item to be inserted (⊙﹏⊙) b

var arr=[1,2,3,4,5,6];

Arr.splice (1,3);
Alert (arr);//[1,5,6]

arr=[1,2,3,4,5,6];
Arr.splice (1,0,6,6,6);
Alert (arr);//[1,6,6,6,2,3,4,5,6]

arr=[1,2,3,4,5,6];
Arr.splice (1,2,6,6,6);
Alert (arr);//[1,6,6,6,4,5,6]
7. Location Method

There are two: IndexOf () and LastIndexOf (). Accept two parameters: the Xiang (optional) lookup starting point to find. Obviously, the first one to start from scratch, the second to start looking at the end. Note that the lookup is strictly equal (= = =).

var arr=[1,2,3,4,5,4,3,2,1];

Alert (Arr.indexof (4)),//3
alert (Arr.lastindexof (4)),//5

alert (Arr.indexof (4,4)),//5
alert ( Arr.lastindexof (4,4));//3

alert (Arr.indexof (10));//-1
8. Iterative Method

First: Every (). Accept two parameters: the function to run on each item and (optionally) the scope object to run the function. The given function accepts three parameters: the value of the array item, the position of the item in the array, and the group object itself. The method runs the given function for each item in the array, and returns true if each entry returns true:

var arr=[1,2,3,4,5,6,7];
var reslut=arr.every (function (item,index,array) {return
    (item>2);
});
alert (result);//false

Second: some (). Similar to every (), the difference is that as long as one item returns TRUE, the result returns TRUE.

var arr=[1,2,3,4,5,6,7];
var reslut=arr.some (function (item,index,array) {return
    (item>2);
});
alert (result);//true

Third: Filter (). The method returns an array that consists of items whose function returns a value of true.

var arr=[1,2,3,4,5,6,7];
var reslut=arr.filter (function (item,index,array) {return
    (item>2);
});
alert (result);//[3,4,5,6,7]

Fourth: Map (). The method returns an array that consists of the results of each function after it is run.

var arr=[1,2,3,4,5,6,7];
var reslut=arr.map (function (item,index,array) {return
    (item*2);
});
alert (result);//[2,4,6,8,10,12,14]

The fifth one is: ForEach (). The method does not return a value, it just runs the function for each item.

9. Merge Method

There are two: reduce () and reduceright (). The method iterates through all the items of the group, and then returns a final value. The former starts from scratch and the latter begins at the end. Accept two parameters: the function to run on each item and (optionally) the initial value as the base of the merge. The function accepts 4 parameters: the previous value, the current value, the index of the item, and the array object. The return value of the function is passed as the first argument to the next item.

var arr=[1,2,3,4,5]; var result=arr.reduce (function (Prev,cur,index,array) {return (prev+
cur);
}); alert (result);//15 
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.