JavaScript Introductory tutorials for reference types _javascript tips

Source: Internet
Author: User
Tags array length prev

Reference type

A reference type is a data structure that is used to organize data and functionality together. It is also often referred to as a class, but it is not appropriate to address it. Although ECMAScript is technically an object-oriented language, it does not have the basic structure of classes and interfaces supported by traditional object-oriented languages. Reference types are sometimes referred to as object definitions because they describe the properties and methods that a class of objects have.

As mentioned earlier, the value of a reference type is an object, in ECMAScript, a reference type is a data structure that organizes data and functionality together, and an object is an instance of a particular reference type.

 
 

It declares an instance of the reference type object and stores the instance in variable a, which means that the variable actually does not contain the instance itself, but rather a pointer to the instance.

For the object type, the common objects literal notation is used to create an instance of both Var a={name: "Nick", age:20} The advantage of doing this is to give people the feeling of encapsulation. The object's access is either a dotted notation or a square bracket notation. A.name is equivalent to a["name", note that the "name" here is represented as a string.

For the array type, you can use the array literal notation.

For the array type, you can use length to change the length of the array. (Add or remove items from the end of the array)

The method of detecting an array is the Array.isarray (value) method

Conversion method: ToString () is converted to a string that splits the items by "." ValueOf (), the return is still an array. toLocaleString () can be implemented using the following example.

var p1={
tostring:function () {return ' Guo ';},
tolocalestring:function () {return ' Yuzhe ';}
}
var p2={
tostring:function () {return ' song ';},
tolocalestring:function () {return ' Hap ';}}
var p=[p1,p2];
Alert (p); Guo,song

Visible alert invokes the ToString () method before the output, in addition to the join () method, which is used to return the specified symbol to a string with the default (not set parameter) as ",".

Stack method: Push () Adds an item at the end, returning the array length. Pop () deletes the item at the end and returns the deleted item.

Queue method: Shift () overflows the first item of the array, returning the item. Unshift () Adds an item to the first paragraph, returning the array length.

Reordering methods (The return value is an array):

Reverse () reverse order. A[LENGTH-1]=A[0]

Sort () Ascending sort the default sort () is in ASCII order, not the size of the number we think it is, so the size needs to be used

function Compare (NO1,NO2) {
if (No1<no2) {
return-1;}
else if (No1>no2) {return
1;}
else{return
0;}
var a=[1,2,3,4,6,5];
A.sort (compare);
Alert (a)

To produce a descending effect, simply reverse the IF statement.

    Action method:

Concat () Creates a copy that has no effect on the original array, adding accepted arguments to the end of the array.

Slice () Creates a copy that accepts 1 or 2 arguments (returns the starting and ending positions of the item, not including the ending position), and returns all items from the specified position to the end, if there is only one argument. If the argument is a negative number, the result is length+arguments, and an empty array is returned if the end position is less than the starting position.

Splice (): 1. Delete Method--Specify two parameters, the position of the first item deleted, and the number of items deleted.

2. Insert Method--Specify three parameters, starting position, 0 (number of deletes), items to insert.

3. Replacement Method--Specify three parameters, starting position, number of deletions, items to insert

The insert/Replace position is the starting position.

    Location Method:

IndexOf () returns the array of items to find below, and returns-1. Parameter: the index (subscript) of the Xiang (optional) lookup point to find.

LastIndexOf () is a reverse order of indexof ().

    Iteration Method:

2 parameters: The function to run and (optionally) scope, the functions passed into these methods need to have three parameters (item (the value of the array item), index (the position of the item), array (array object itself)).

Every () each item in the array runs the given function, and returns TRUE if each entry returns ture

Filter () returns an array of items that return true

ForEach () runs the given function for each item, no return value

Map () returns an array of results for each function execution

Some () returns true if one of the entries is true

<script>
var a=[1,2,3,4];
var b=a.every (function (item,index,array) {return
item>2;
});
alert (b); False
</script>

    Merge method:

Reduce () starts with the first entry of the number, and Reduceright () traverses the last item in the array

Use reduce () to find all and in the array

<script>
var a=[1,2,3,4];
var b=a.reduce (function (prve,cur,index,array) {return
prve+cur;
});
alert (b); 10

At the first execution, the prev is 1,cur to 2 and the second time the prev is 3,cur to 3.

The above is a small series to introduce the JavaScript Introductory tutorial of the reference type of the relevant content, I hope to help you!

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.