Two monolithic built-in objects are defined in JavaScript: Global and Math.
Global Objects
Global object is the most special object in JavaScript. Attributes and methods that do not belong to any other object are ultimately their properties and methods. In fact, there are no global variables or global scopes, and all attributes and functions defined in the global scope are properties of Global objects.
Global objects contain some useful methods:
1. URI encoding method
The encodeURI() and encodeURIComponent() methods of the Global object can encode the URI. encodeURI() is mainly used for the entire URI, while encodeURIComponent() is mainly used for encoding a certain segment in the 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 belong to the URI itself (such as colons, forward slashes, question marks, and pound signs), encodeURIComponent encodes any non-standard characters found.
There are two decoding methods corresponding to encodeURI() and encodeURIComponent().
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
Among them, decodeURI() can only decode characters replaced with encodeURI(). decodeURIComponent can decode encodeURIComponent().
2.eval() method
eval() only accepts one parameter, that is, the JavaScript string to be executed, for example:
eval("alert('hello')");
The above line of code is equivalent to:
alert("hello");
When the parser calls the eval() method, the passed parameters will be parsed as the actual JavaScript statement, and the execution result will be inserted into the original location. The code executed by eval() is considered to be part of the execution environment containing the call, so the executed code has the same scope chain as the execution environment. This means that the code executed via eval() can refer to variables defined in the inclusion environment.
var msg = "good morning";eval("alert(msg)"); // "good morning"Similarly, a function can be defined in eval() and then referenced outside of the call:
eval("function saysHi() {alert('hello')}");The same is true for variables:
eval("var msg = 'hello world'");alert(msg); // "hello world"No variables or functions created in eval() are promoted, and they are included in a string when parsing the code; they are created only when eval() is executed.
3.Window object
JavaScript does not indicate how to access the Global object directly, but web browsers implement 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 saysColor() { alert(window.color);}window.sayColor();The above defines a global variable color and the global function sayColor() method. The color variable is accessed through window.color inside the function, indicating that the global variable color is a property of the window object. Then call the sayColor() method through window.sayColor(), indicating that sayColor() is a method of the window object.
Method to obtain Global object:
var global = function () { return this;}();Math object
JavaScript provides Math objects for fast computing capabilities.
1. Properties of Math object
Most of the properties of Math objects 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 in a set of numeric values. Both methods can receive as many numerical parameters.
var max = Math.max(4,89,65,34);alert(max); // 89var min = Math.min(4,89,65,34);alert(min);
To find the maximum and minimum values in the numeric value, you can call the apply() method in the following way:
var values = [4,89,65,34];var max = Math.max.apply(Math, values);
3. Rounding method
• Math.ceil(): Round upward, that is, to go upward, as long as the decimal digit is not 0, round upward
• Math.floor(): Round down, that is, rounding method, discarding the decimal places
• Math.round(): Standard rounding, that is, rounding method
Example:
alert(Math.ceil(11.4)); // 12alert(Math.ceil(11.5)); // 12alert(Math.ceil(11.8)); // 12alert(Math.floor(11.4)); // 11alert(Math.floor(11.5)); // 11alert(Math.floor(11.8)); // 11alert(Math.round(11.4)); // 11alert(Math.round(11.5)); // 12alert(Math.round(11.8)); // 12alert(Math.ceil(-11.4)); // 12alert(Math.ceil(-11.4)); // -11alert(Math.ceil(-11.5)); // -11alert(Math.ceil(-11.8)); // -11alert(Math.floor(-11.4)); // -12alert(Math.floor(-11.5)); // -12alert(Math.floor(-11.8)); // -12alert(Math.round(-11.4)); // -11alert(Math.round(-11.5)); // -11alert(Math.round(-11.8)); // -12alert(Math.round(-11.4)); // -11alert(Math.round(-11.5)); // -11alert(Math.round(-11.8)); // -12
4.random() method
The Math.random() method returns a random number (0≤r<1).
For example, get an integer between 1 and 10:
var num = Math.floor(Math.random() * 10 + 1);
5. Other methods
The Math object also provides some simple or complex calculations that complete various simple or complex calculations.
ECMA-262 specifies these methods, but different implementations may vary in accuracy.
The above article in-depth understanding of JavaScript single-body built-in objects is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.