One, the global object
The global (Global) object is a special object in ECMAScript because the object does not exist. Properties and methods that are not part of any other property and method in ECMAScript belong to it. So, in fact, there are no global variables and global functions; all variables and functions defined at the global scope are properties and methods of the Globals object.
ECMAScript does not define how to call the global object, so the global. property or Global. Method () is not valid.
(The Web browser implements global as part of the Window object)
The global object has some built-in properties and methods.
var box = '//lee lee ';
Alert (encodeURI (box)); Encoding Chinese only
Alert (encodeuricomponent (box)); Coded special characters and Chinese
var a = encodeURI (box);
var b = encodeuricomponent (box);
Alert (decodeURI (a)); decodeURI decoding
Alert (decodeuricomponent (box)); decodeURIComponent decoding
The eval () method acts primarily as a string parser, and he accepts only one parameter, which is the string of JavaScript code to execute.
Eval (' var box = 100 '); Parse the string code
alert (box);
Eval (' Alert (100) ');
Eval (' function box () {return 123} '); You can also parse functions
Alert (Box ());
Two. Math Object
ECMAScript also provides an object, the math object, for saving mathematical formulas and information. The Math object provides a much faster execution of the computational functionality than we do with the direct programming of JavaScript.
alert (MATH.E);
alert (MATH.PI);
Min () and Max () methods
Alert (Math.min (2,5,8,4,9,2,0,1)); Minimum value
Alert (Math.max (2,5,8,4,9,2,0,1)); Maximum Value
Rounding Method
Rounding up values rounded up to the nearest integer
Alert (Math.ceil (25.9));
Alert (Math.ceil (25.5));
Alert (Math.ceil (25.1));
Round down to round a number down to the nearest integer
Alert (Math.floor (25.9));
Alert (Math.floor (25.5));
Alert (Math.floor (25.1));
Rounding rounded values rounded to the nearest integer
Alert (Math.Round (25.9));
Alert (Math.Round (25.5));
Alert (Math.Round (25.1));
The Math.random () method returns a random number between 0 and 1, excluding 0 and 1. If you want to be larger than this range, you can apply the formula:
Value = Math.floor (Math.random () * total + first value)
Alert (Math.random ());
for (var i=0; i<10; i++)
{
document.write (Math.random ());
document.write (' <br/> ');
}
Alert (Math.floor (Math.random () *10+1));//randomly generates an arbitrary number between 1-10
for (var i=0; i<10; i++)
{
document.write (Math.floor (Math.random () *10+1));
document.write (' <br/> ');
}
//returns between 5-10
for (var i=0; i<10; i++)
{
document.write (Math.floor (Math.random () *10+5));//Range 5-14 between 10+5-1=14 (5-14)
document.write (' <br/> ');
}
//If you want to be between 5-10, 10-5+1=6
for (var i=0; i<10; i++)
{
document.write (Math.floor (Math.random () *6+5));//Range 5-10 between 6+5-1=10 (5-10)
document.write (' <br/>
‘);
}
//The above method is too cumbersome and you need to calculate the start value. You can write a function to calculate the start value
function Select (start,end)
{
var total = end-start+1;
Return Math.floor (Math.random () *total+start);
}
//When calculating, simply write the parameters of the start and end values in the function.
for (var i=0; i<10; i++)
{
document.write (select (5,10));
document.write (' <br/>
‘);
}
Alert (Math.Abs (-5)); Returns the absolute value of num
Alert (MATH.SQRT (9)); Returns the square root of num
JS built-in objects