JS built-in objects

Source: Internet
Author: User

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

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.