JavaScript defines two built-in objects: Global and Math.
First, global object 1.URI encoding method
The encodeURI () and encodeURIComponent () methods of the global object can encode the URI for sending to the browser. A valid URI cannot contain certain characters, such as a space, and the two URI encoding method can encode the URI and replace all invalid characters with a special UTF-8 encoding, allowing the browser to accept and understand.
The main difference between the two methods is that encodeURI () does not encode special characters for which this book is a URI, such as: colons, forward slashes, question marks, and well numbers.
encodeURIComponent () encodes any non-standard characters it discovers.
var uri = "http://www.baidu.com"; var str1 = encodeURI (URI); var str2 = encodeURIComponent (URI); alert (STR1); http://www.baidu.com alert (STR2);//http%3a%2f%2fwww.baidu.com alert (decodeURI (STR1));//http:// www.baidu.com alert (decodeuricomponent (STR2));//http://www.baidu.com
2.eval () method
The eval () method is a complete JavaScript parser that accepts only one parameter, the JavaScript string to execute. When the parser discovers that the eval () method is called in the code, it parses the passed-in parameter as the actual JavaScript statement, and then inserts the execution result into the original location.
var str = "var a = 10; var B = ten "; eval (str); alert (a + B); eval ("function say () {alert (' Hi ');}"); Say ();
Two, Math object 1.min and Max methods
These two methods are used to determine the maximum and minimum values in a set of numeric values, which can accept any number of numeric parameters.
var max = Math.max (11,1,2,20,3); alert (max); 20var min = math.min (11,1,2,20,3); alert (min); 1
These two methods are often used to avoid redundant loops and to determine the maximum and minimum values for a set of numbers in an if statement. The following apply () method can be used to find the maximum value of an array.
var arr = [9,0,8,1,7,2,6,3,5,4];var max = Math.max.apply (Math,arr); var min = Math.min.apply (Math,arr); alert ("max =" + Max ); 9alert ("min =" + min); 0
2.random () method
The Math.random () method returns a random number between 0 and 1, excluding 0 and 1.
value = Math.floor (Math.random () * Total number of interval values + first value)
For example 2 to 10, a total of nine numbers. It can be written like this.
var num = Math.floor (math.random * 9 + 2);
function Randomnum (low,uper) {var a = Uper-low + 1;return Math.floor (math.random () * a + low);} var num = randomnum (2,10), alert (num); A number between 2 and 10
Other methods of the math class
Iii. Date Type 1. creating objects
var date = new Date ();
2. Common methods
getDate (): returns one day in one months (1 ~ to)
GetDay (): returns one day of the week (0 ~ 6)
Strong>getmonth (): returns the month (0 ~ one)
getfullyear (): returns the year in four digits
GetTime (): returns the number of milliseconds from January 1, 1970 to the present
Parse (): returns the number of milliseconds from midnight on January 1, 1970 to the specified date (string)
tolocalestring (): converts a Date object to a string based on the local time format.
tolocaledatestring (): converts the date part of a Date object to a string based on the local time format
var date = new Date (); alert (date); Wed Apr 17:46:26 Gmt+0800alert (date.tolocalestring ());//2016/4/27 PM 5:50:37alert (date.tolocaledatestring () );//2016/4/27var year = Date.getfullyear (), var month = Date.getmonth () + 1;var day = Date.getdate (); var hour = Date.getho Urs (); var minute = Date.getminutes (); var seconds = date.getseconds ();//Beijing time: April 27, 2016 17:59:47alert ("Beijing Time:" + year + "Years") + month + "months" + Day + "days" + Hour + ":" + Minute + ":" + seconds);
JavaScript Basic Learning (v)-Other reference types