Deep understanding of JavaScript monomer built-in objects _javascript tips

Source: Internet
Author: User
Tags numeric value

There are two monomer built-in objects defined in JavaScript: Global and math.

Global Object

The global object is one of the most specific objects in JavaScript. Properties and methods that are not part of any other object, and ultimately their properties and methods. In fact, there are no global variables or global scopes, and all the properties and functions defined in the global scope are properties of the global object.

The global object contains a number of useful methods:

1.URI Coding Method

The encodeURI () and encodeURIComponent () methods of the global object can encode the URI, encodeURI () primarily for the entire URI, and encodeURIComponent () is primarily used to encode a paragraph in a URI.

var uri = "Http://www.jb51 xxyh.com#login";
Alert (encodeURI (URI));     "Http://www.jb51%20xxyh.com#login"
alert (encodeURIComponent (URI));//"Http%3a%2f%2fwww.jb51%20xxyh.com%23login"

encodeURI () does not encode special characters that are part of the URI (such as colons, forward slashes, question marks, and well numbers), and encodeuricomponent encodes any non-standard characters found.

There are two decoding methods corresponding to encodeURI () and encodeURIComponent () decodeURI and decodeURIComponent

var uri = "Http%3a%2f%2fwww.jb51%20xxyh.com%23login";
Alert (decodeURI (URI));   "Http%3a%2f%2fwww.jb51 xxyh.com%23login"
alert (decodeURIComponent (URI));//Http://www.jb51 Xxyh.com#login

where decodeURI () can only decode characters that are replaced with encodeURI (). The decodeuricomponent can decode the encodeURIComponent ().

2.eval () method

Eval () takes only one parameter, which is the JavaScript string to execute, for example:

Eval ("alert (' Hello ')");

The above line of code is equivalent to:

Alert ("Hello");

When the parser calls the eval () method, the incoming argument is parsed as the actual JavaScript statement, and the execution results are inserted into the original location. Code executed through eval () is considered part of the execution environment that contains the call, so the executed code has the same scope chain as the execution environment. This means that code executed through eval () can refer to variables defined in the containing environment.

var msg = "Good Morning";
Eval ("Alert (msg)");    "Good Morning"

Similarly, you can define a function in eval () and then reference the function outside of the call:

Eval ("function Sayhi () {alert (' Hello ')}");

The same is true for variables:

Eval ("var msg = ' Hello World '");
Alert (msg);    "Hello World"

Any variables or functions created in eval () are not promoted, and are included in a string when parsing the code, which is only created when Eval () is executed.

3.window objects

JavaScript does not indicate how to access the global object directly, but the Web browser implements it as part of the Window object. Therefore, all variables and functions declared in the global scope are called properties of the Window object.

var color = "Red";
function Saycolor () {
  alert (window.color);
}
Window.saycolor ();

The above defines a global variable color and global function Saycolor () method, which accesses the color variable inside the function by Window.color, stating that the global variable color is the property of the Window object. The Saycolor () method is then invoked by Window.saycolor () to show that Saycolor () is the method of the Window object.

Methods to get the global object:

var global = function () {return this
  ;
} ();

Math Object

JavaScript provides a math object to provide fast computing capabilities.

Properties of 1.Math Objects

Most of the properties of the Math object are special values in some mathematical calculations.

2.min () and Max () methods

The min () and Max () methods are used to determine the minimum and maximum values for a set of values. Both of these methods can receive any number of numeric parameters.

var max = Math.max (4,89,65,34);
Alert (max);  The

var min = math.min (4,89,65,34);
Alert (min);

To find the maximum and minimum values in a numeric value, you can invoke the Apply () method in the following ways:

var values = [4,89,65,34];
var max = Math.max.apply (Math, values);

3. Rounding Method

Math.ceil (): Rounding up, in one way, as long as the decimal bit is not 0 to take up the whole
Math.floor (): Rounding down, that is, take the whole method, shed the decimal places
Math.Round (): Standard rounding, that is rounding method

Example:

Alert (Math.ceil (11.4));
alert (Math.ceil (11.5));//
alert (Math.ceil (11.8));//

alert (Math.floor (11.4))
; Alert (Math.floor (11.5)); One alert (
Math.floor (11.8));//

alert (Math.Round (11.4));/
alert (Math.Round (11.5));
Alert (Math.Round (11.8));

alert (Math.ceil ( -11.4));// -11
alert (Math.ceil ( -11.5));// -11
alert (Math.ceil (-11.8));//-11

alert (Math.floor ( -11.4));// -12
alert (Math.floor ( -11.5));// -12
alert (Math.floor (-11.8));//-12

alert (Math.Round ( -11.4));// -11
alert (Math.Round ( -11.5));// -11
alert (Math.Round (-11.8));//-12

4.random () method

The Math.random () method returns a random number (0≤r<1).

For example, get an integer from 1 to 10:

var num = Math.floor (math.random () * 10 + 1);

5. Other methods

The Math object also provides a number of simple or complex computations to complete.

ECMA-262 prescribes these methods, but different implementations may have different precision.

The above in-depth understanding of JavaScript monomer built-in object is small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

Related Article

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.