Methods in an Object

Source: Internet
Author: User

1. Conversion method

objects or arrays have tolocalestring (), toString (), and valueof () methods. where ToString () and valueof () will return the same value regardless of who they rewrite, the array will use each value in string form, separated by commas;

var box = [' Depressed ', 28, ' Wuhan '];alert (box); alert (box.tostring ()); alert (box.valueof ()); alert (box.tolocalestring ());

1.1join () method (you can use a different semicolon character to build the string)

var box = [' Depression ', 88, ' computer Programming '];alert (Box.join (' | ')); /' Depression ' |88| ' Computer Programming '

2. Stack method (Advanced post-exit)

Push () and pop ()

var box = [' Depression ', 88, ' computer Programming '];alert (Box.push (' unrivaled ')); The array box adds an element and returns the length of alert (box);//view array box.pop ();//Move the divisor group box at the end of the element and return the removed element to alert (box);//view element

3. Queue method

Add an element to the end of the array via push (), and then remove an element from the front end of the array through the shift () method.

var box = [' Depression ', 88, ' computer Programming '];alert (Box.push (' unrivaled ')); The array box adds an element and returns the length of alert (box);//view array alert (Box.shift ());//Move the divisor group box to start the element and return the removed element to alert (box);//view element

The Unshift () method adds an element to the front end of the array. ECMAScript also provides an array of unshift () methods, which are the exact opposite of the shift () method.

var box = [' Depression ', 88, ' computer Programming '];alert (Box.unshift (' unrivaled ')); An array of box starts with an element that returns an array of length alert (box);//view array alert (Box.pop ());//Move the divisor group box to start the element and return the removed element to alert (box);//view element

The Ps:ie browser to the Unshift () method always returns undefined instead of the new length of the array.

So the return value of unshift () I do not use.

4. Rearranging methods

Reverse () counter-attack arrangement

var box = [1,2,3,4,5];//array alert (Box.reverse ()); Reverse order method, returns the sorted array alert (box);//The original array is also sorted in reverse order, indicating the application

Sort () arrange from small to large

var box = [4,1,5,3,8,6];//array alert (Box.sort ());//From small to large sort, return sorted array alert (box);//The original array is sorted from small to large

The default ordering of the sort method has some problems with numeric sorting, because the algorithm for sorting numbers and numbers is
The same. We have to modify this feature and modify it by passing a function parameter to the sort (parameter) method.
Refer to the manual for instructions.

function Compare (value1,value2) {if (value1<value2) {return-1;} else if (value1>value2) {return 1;} Else{return 0;}} var box = [2,1,15,5,10];//var box = [' 2 ', ' 1 ', ' 15 ', ' 5 ', ' 10 ',];//array alert (Box.sort (Compare));//From small to large sort, return sorted array alert (box) ;//The original array is also sorted from small to large
How to do it from big to small
Alert (Box.recerse ());//from large to small

5. How to Operate

The 5.1concat () method (The Concat () method can create a new array based on the current array)

var box = [' Depressed ', ' Wuhan '];
var box2 = Box.concat (' computer programming ');//create a new array and add a new element
alert (BOX2);
alert (box); The current array is not changed any more

The 5.2slice () method can get the specified range element based on the current array and create a new array.

var box = [' Depressed ', ' Wuhan ', ' LC ', ' lx '];var box2 = box.slice (0); var box3 = Box.slice (2,3);//Get 3 to 4 element alert (BOX2); alert ( BOX3); alert (box);//The original array does not change

The main purpose of 5.3splice () is to insert elements into the middle of the array.

Delete function in splice ()

var box = [' Depressed ', ' Wuhan ', ' LC ', ' lx '];var box2 = Box.splice (0,2);//first to third element, first and second element alert (BOX2);//Returns the intercepted element alert (box );//The element that the current array is intercepting is deleted.

Insert function in splice ()

var box = [' Depressed ', 28, ' wuhan '];var box2 = Box.splice (0,2, ' I'll always be your big uncle ', ' Mydog ');//delete element 1th to 3rd and insert new element ' I'll always be your uncle ', ' mydog ' alert (box2); alert (box);//The original array box changed

Splice () replaces the function more

var box = [' Li Tinghui ', 28, ' Yancheng ']; Current array var box2 = Box.splice (1,1,100); Interception of the 2nd, replaced by 100alert (BOX2); Output intercept 28alert (box); Output array

Methods in an Object

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.