JavaScript notes and summaries (2-4) JavaScript built-in objects

Source: Internet
Author: User
Tags local time rounds square root

①string String Object

fromCharCode () static method that creates a new string with the character code passed as a parameter.
length The length of the string.
CharAt () extracts the character at the specified position in the string.
charCodeAt () returns the character encoding at the specified position in the string.
concat () Connect one or more values to a string.
indexOf () Retrieves a character or a substring in a string.
LastIndexOf () retrieves a character or a substring backwards in the string.
Localecompare () compares two strings in a local specific order. Match () performs pattern matching with regular expressions.
replace () Performs a find, replace operation with a regular expression.
Search () retrieves substrings in a string that match a regular expression.
Slice () returns a fragment of a string or a substring.
Split () splits the string into an array of strings and splits at the specified cutoff character or regular expression.
SUBSTRING () extracts a substring from a string.
substr () Extracts a substring from a string.
toLowerCase ()Converts all characters in a string to lowercase, and then returns a copy.
ToString () returns the original string value.
toUpperCase ()Converts all characters in a string to uppercase, and then returns a copy.
ValueOf () returns the original string value.

"Example" indexOf (), retrieving a character or a substring in a string, and returning the position of a character or string

<script>var str = "PhpMyAdmin"; // Find out if there is PHPConsole.log (str.indexof ("PHP") in the string); Console.log (Str.indexof ("PHP") >-1? " Find ":" Not find "); // Find out if there is a Pythonconsole.log (str.indexof ("Python")) in the string,Console.log (Str.indexof ("python") >-1? " Find ":" Not find "); </script>

Output:

0 (position is 0)

Find

-1 (not found)

Not find

②date Date Object

GetDate () Returns a day of the month.
GetDay () Returns the day of the week.
getfullyear () Returns the year in the date. (4-bit)
getUTCFullYear () ibid.
getHours () Returns the hour field of an object.
getUTCHours () ibid.
getmilliseconds () Returns the millisecond field of an object.
getUTCMilliseconds () ibid.
getminutes ()Returns the minute field of an object.
getUTCMinutes () ibid.
Getmouth ()Returns the month field of the object. (Range is 0-11)
Getutcmouth () ibid.
getseconds () Returns the second field of an object.
getUTCSeconds () ibid.
GetTime () returns the millisecond representation inside the object.
getTimezoneOffset () returns the difference between the local time of this date and the UTC representation, in minutes.
getYear ()It is recommended to use getFullYear (). (2-bit or 4-bit)
SetDate () Sets the day of the object in the month.
SetUTCDate () ibid.
setFullYear () Sets the year field in the object.
setUTCFullYear () ibid.
Sethours () Sets the hour field for the object.
setUTCHours () ibid.
Setmilliseconds () Sets the millisecond field of the object.
setUTCMilliseconds () ibid.
Setminutes () Sets the minute field of the object.
setUTCMinutes () ibid.
Setmouth () Sets the month field of the object.
Setutcmouth () ibid.
Setseconds () Sets the second field of the object.
setUTCSeconds () ibid.
SetTime () Sets the individual fields of the object in milliseconds.
Setyear () recommends the use of setFullYear ().
toDateString () returns a string representation of the date part of the date.
toGMTString () recommends the use of toutcstring ().
Tolacaledatastring () returns a string representation of the date part of the date.
toLocaleString () Converts the object to a string.
Tolacaletimestring () returns a string representation of the time portion of the date.
ToString () Converts the object to a string.
toTimeString () Converts the object to a string.
ToString () returns the string representation of the time portion of the date.
toUTCString () Converts the object to a string.
ValueOf () Converts the object to its internal millisecond format.
The parse () static method, parsing the string representation of the date and time, returns its internal millisecond representation.
The UTC () static method that returns the millisecond representation of the specified UTC date and time.

The Date object needs to be explicitly manufactured. var dt = new Date ();

"Example" getFullYear ()

<script>varnew  Date (); var y = dt.getfullyear (); Console.log (y); </script>

Output: 2015

Show time on "Example" page

<script>functionsettime () {varDay= ""; varMonth= ""; varAmpm= ""; varAmpmhour= ""; varMyweekday= ""; varYear= ""; varMyhours= ""; varMyminutes= ""; varMyseconds= ""; varDT =NewDate (); Myweekday=Dt.getday (); Mymonth=parseint (Dt.getmonth () +1) <10? " 0 "+ (Dt.getmonth () +1):d t.getmonth () +1; Myday=dt.getdate (); Myyear=dt.getyear (); Myhours=dt.gethours (); Myminutes=dt.getminutes (); Myseconds= parseint (Dt.getseconds ()) <10? " 0 "+dt.getseconds ():d t.getseconds (); year= (Myyear > 200)? myyear:1900 +myyear; if(Myweekday = = 0) Weekday= "Sunday"; Else if(Myweekday = = 1) Weekday= "Monday"; Else if(Myweekday = = 2) Weekday= "Tuesday"; Else if(Myweekday = = 3) Weekday= "Wednesday"; Else if(Myweekday = = 4) Weekday= "Thursday"; Else if(Myweekday = = 5) Weekday= "Friday"; Else if(Myweekday = = 6) Weekday= "Saturday"; Datetime.innertext=year+ "Year" +mymonth+ "month" +myday+ "Day" +myhours+ ":" +myminutes+ ":" +myseconds+ "" +weekday; SetTimeout ("SetTime ()", 1000);} SetTime ();</script>

Output: April 25, 2015 13:12:49 Saturday (automatically show new time per second)

③math Mathematical Objects

Math.PI    constant π.
ceil (numeric) Rounds a number (the smallest integer greater than or equal to the number).
Floor (value) Rounds to a number (the largest integer less than or equal to the number).
Max (numeric 1, value 2) Returns the maximum value.
min (value 1, value 2) Returns the minimum value.
Calculates the numeric value of 1 by 2 times.
random () Returns the random number 0-1.
Rounded.

sqrt (numeric) Open square root.

The Math object does not need new, directly mtah. Properties (like static methods in PHP, do not need to instantiate a method directly in a static Class). The methods of Math are all static methods.

"Example" Ceil

<script>Console.log (Math.ceil (10.1)); </script>

Output: 11

"Example" random return [0,1)

<script>// return [0,1] random number Console.log (Math.random ()); // returns the random number of [5,10] Console.log (Math.random () * 5 + 5); </script>

Output: (e.g.)

0.05129604251123965

7.089222565991804

④ Array Objects

The length array contains the number of elements.
concat () Adds an element to the array (the value of this operation's original array is unchanged).
Join ()Converts all the elements in the array into strings, and then joins them.
pop ()Deletes and returns the last element of the array.
push ()Adds an element to the end of the array, returning the value to the new length (in bug:ver1.2, which returns the last element of the array).
reverse () Reverses the order of the elements in the array on the original array.
shift ()Deletes and returns the head element of the array.
Slice ()Returns a subarray of arrays that do not modify the original array (Bug: Start cannot be negative in IE4).
sort ()Sorts the arrays from the original array.
Splice () Insert, delete, replace an array element.
Tolocalstring () converts the array into a local string.
ToString () converts the array into a string.
unshift () Inserts an element at the head of the array and returns the new length of the value array.

(There are also OBJ objects, Argument objects, Boolean objects, number objects, REGEXP objects)

Reference:

JavaScript Core Reference tutorial built-in objects

JavaScript notes and summaries (2-4) JavaScript built-in objects

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.