Singleton built-in object: Any object provided by an ECMAScript implementation, independent of the host environment, and existing at the beginning of execution of an ECMAScript program. That is, the built-in object itself is already instantiated. Common built-in objects are: Object, Array, String, Global, and Math.
Global
It is the most special object because code does not explicitly access it. In fact, both Global variables and Global functions become properties of the Global. IsNaN (), isFinite(), parseInt(), and parseFloat() are all actually Global methods.
Encoding and decoding
let url="https://www.wrox.com/illegal value.js#start"
encodeURI(url) //https://www.wrox.com/illegal%20value.js#start
encodeURIComponent(url) //https%3A%2F%2Fwww.wrox.com%2Fillegal%20value.js%23start
Copy the code
The eval () method
It is a complete ECMAScript interpreter that takes a single parameter, the ECMAScript string to be executed:
eval("console.log('hello')") //hello
Copy the code
In general, the code that eval() executes belongs to the context in which the call is made, and the code executed has the same scope chain as the context:
let msg="hello"
eval("console.log(msg)") //hello
Copy the code
eval("function sayHi(){console.log('hi')}")
sayHi() //hi
Copy the code
eval("let msg="hello") console.log(msg) //helloCopy the code
In strict mode, executing eval() creates a new context. In this case, the outside cannot access the inside, but the inside can access the outside.
Global object properties
The window object
Although Global cannot be accessed directly, the browser proxies the Window object for the Glocal object. Therefore, all variables and functions declared in the global scope become properties of the window.
Get the Global object
When a function is executed without explicitly specifying this, this is equal to the Global object.
let global=function(){
return this} ();Copy the code
Math
ECMAScript provides Math objects as a place to store mathematical formulas, information, and calculations.
Math properties
These properties are mainly used to hold special values in mathematics:
Math methods
Take the maximum and minimum
Math.min(1.2.3.4.5) / / 1
Math.max(1.2.3.4.5) / / 5
// You can use the extension operator for arrays
let arr=[1.2.3.4.5]
Math.max(... arr)/ / 5
Copy the code
rounding
let num=1.5
Math.ceil(num) / / 2
Math.floor(num) / / 1
Math.round(num) / / 2
Copy the code
The random number
Math.random() / / 0.932879007220689
/ / 1 ~ 10
Math.floor(Math.random()*10+1)
// Take 2 to 9:* indicates the total number, + indicates the minimum number
Math.floor(Math.random()*9+2)
Copy the code