JS common built-in objects and methods

Source: Internet
Author: User

everything is object in js, string, array, value, function ...

Built-in objects have their own properties and methods, which are accessed in the following ways:

Object Name. Property name;

Object Name. Method Name

1.Array Array Objectunshift () array start increment

Function: add one or more to the beginning of the array

Parameters: one or more

Return value: the length of the array

The original array has changed

shift () Array begins by deleting an item

Function: deletes an array at the beginning of the

Parameters: None

Return value: the content that was deleted

The original array has changed

The end of the push () array is incremented

Function: add one or more items to the end of the array

Parameters: one or more

Return value: the length of the array

The original array has changed

deletes an item at the end of the pop () array

Function: one item is not deleted at the end of the array

Parameters: None

Return value: the content that was deleted

The original array has changed

concatenation of concat () arrays

Ary1.concat (ary2,ary3 ...)

Concat can be used to achieve the cloning of arrays, Concat () does not pass parameters

Splice (index, howmany, item1, ... itemx)

Splice can be used to delete, increment and replace the array according to the Parameters.

The first two parameters, index and howmany, are required parameters, followed by optional parameters

Splice (index, 0, item1, item2 ...) Increase

Increment from index start, insert added content to index front

Splice (index, N) Delete

Delete N from index, if only one parameter, splice (index), is removed from the Index.

Splice (index, n,item1,item2 ... ) Replace

Replace n with the index, the content of item1, item2 ....

Slice (n,m) intercept

Intercept from index n to index m but not m, the original array does not change

Slice (0) or splice () enables cloning of arrays

Reverse () Array Rollover

The return value is a new array after flipping, and the original array has changed

sort () Array sorting

How to: sort (function (a, b) {return-a-line}) from small to large

Sort (function (a, b) {return B-a}) from large to small rows

toString () Array goto string

Convert an array to a comma-delimited string

join (splicing Form) Stitching

Concatenation of the array into other forms of the split string, with the Eval () can be used to achieve the mathematical operation Eval (join (' + '))

Common but incompatible methods of arrays:indexOf (find what) Find

Ary.indexof (find What) finds an item in the array, returns the Item's lead, and returns no 1;

ForEach () Traversal
foreach receives two parameters, one Callback,thisarg
Callback receive three Parameters: 1) Item 2) Index 3) input
Thisarg used to change the this point in the callback;
ForEach has no return value, but map has a return value

Map () Traversal

2.string String

CharAT (index) Find characters by index

charCodeAt (index) found by index the Unicode encoding of the Character. This return value is an integer between 0-65535.

method charCodeAt () is similar to the operation performed by the CharAt () method, except that the former returns the encoding of the character at the specified position, and the latter returns a character string.

indexOf () Search from the back, find the index of the returned content, can not find the return-1;

lastIndexOf () looking forward from the back, find the index of the returned content, can not find the return-1;

Slice (n,m) Search from index n to index m but excluding m,slice can take negative values

substring (n,m) find to index m from index n, but not m, negative values are not allowed

substr (n,m) interception of M from index n

Split (cut Form) splits a string into a string array.

touppercase () Turn capital letters

toLowerCase () Turn lowercase letters

3.MathObject

Math.floor () down rounding

Math.ceil () rounding up

Math.random () take a random decimal between 0-1

Math.Round () rounding

math.abs () Take absolute value

Y power of Math.pow ( x, y) x

math.sqrt () Open Square

Math.max () Take maximum value

math.min () take the minimum value

4.Date Date Object

new Date () creates a day object

getfullyear () return year

GetMonth () Returns the number of months (0-11) that you want to get for a month, plus a

GetDay () Returns the day of the week (0-6) and wants to get the week, need to add a

getDate () return Day

getHours () when returned

getminutes () returns minutes

getseconds () returns seconds

getTime () Returns the number of milliseconds from January 1, 1970 00:00 to the present (gmt), which is the timestamp

setyear (yearint) sets the year. 2-digit or 4-digit number


setfullyear (yearint) sets the year. 4 digits

setmonth (monthint) Set Month (0-11)


setDate (dateint) Set Day (1-31)


sethours (hourint) Set Hours (0-23)


setminutes (minint) set number of minutes (0-59)


setseconds (secint) set number of seconds (0-59)


setmilliseconds (milliint) Set Ms (0-999)

JS common built-in objects and methods

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.