This is the 13th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021″

🤔 1. What are built-in objects

Any object provided by ECMAScript, independent of the host environment, and existing at the time of ECMAScript execution. String, Object, Array, Number, and Boolean are all built-in objects.

It exists at execution time, that is, we do not need the instantiated built-in object to be displayed because it is already instantiated. For example, when defining a string type:

let str = "aaa"
str.substring(2)
Copy the code

Let STR = new String(“aaa”) so you can use.substring()

This lesson introduces two other singleton built-in objects: Global and Math. I’ve been trying to figure out what “singleton” means. But I read a lot of books without much explanation. Just remember that these two are singleton built-in objects.

Global

We may not know about Global, but we certainly know about Windows. Take your time

Out the object

The code doesn’t explicitly access it. As a backstop object, it targets properties and methods that do not belong to any object.

There are no global variables

In fact, there are no global variables or functions. Variables and functions defined in the Global scope become properties of the Global object. For example: isNaN(), isFinite(), parseInt(), parseFloat(), etc.

URL encoding method

Let’s start with urls and URIs

URI

Uniform Resource Identifier, the keyword is identifier, for example: a well-known male star surnamed Wu is suspected of rape. We know that there is a male star, surnamed Wu, who is guilty of rape.

Every resource available on the Web, such as HTML documents, images, video clips, programs, etc., is located by a URI. A URI consists of three parts: (1) Naming mechanism for accessing resources, (2) host name for storing resources, and (3) The name of the resource. The URI is represented by a path, emphasizing the resource.Copy the code

URL

Unified resource locator, the keyword is positioning, such as: a well-known male star, one of the four returned sons, Canadian Chinese, wu Mou Fan, the mentor of “The Hip-hop of China”, suspected of raping female fans, chaoyang branch was arrested according to law. That gives us a bead on this guy. A URL is a subset of a URI, just like a URI is a toothpick, and a URL represents a Toothpick in Canada.

A URL is a string of characters used to describe information resources on the Internet. It is used in various WWW client and server programs, especially the famous Mosaic program. Using URLS can use a unified format to describe various information resources, including files, server addresses and directories. The URL consists of three parts: protocol (or service mode), IP address (sometimes including port number) of the host where the resource resides, and specific address of the host resource. Such as directory and file nameCopy the code

Back to the point. Here we need to understand four methods

let uri = "http://www.wrox.com/illegal value.js#start"
/ / code
// encodeURI simply replaces the space with %20
// encodeURIComponent replaces all non-alphabetic characters with the corresponding encoding
encodeURI(uri)  // 'http://www.wrox.com/illegal%20value.js#start'
encodeURIComponent(uri) //'http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.js%23start'

/ / decoding
// 'http%3A%2F%2Fwww.wrox.com%2Fillegal value.js%23start'  
// 20% will be replaced with Spaces but 23% will not be replaced with #
decodeURI('http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.js%23start')
// 'http://www.wrox.com/illegal value.js#start'
decodeURIComponent('http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.js%23start')
Copy the code

Escape () and unescape() have been replaced by the above methods and are obsolete.

eval()

The eval() function evaluates a JavaScript string and executes it as script code.

If the argument is an expression, the eval() function executes the expression. If the argument is a Javascript statement, eval() executes the Javascript statement.

For example, if we execute the following code on the console, the result will be displayed on our page.

eval("document.write('2+2')")
Copy the code

Code executed in eval is added to the context. So you can use external variables in eval. Similarly, the outside can eval everything inside

let msg = "hello"
eval("console.log(msg)")

eval("function say(){console.log('hello')}")
say()
Copy the code

Global object properties

The window object

Although ECMA does not specify how Global is accessed. But the browser implements the Window object as a proxy for the Global object. All variables and functions declared in the global scope become properties of the window.

var color ="red"
function sayColor(){
    console.log(window.color)
}
window.sayColor()  // "red"
Copy the code

Calling the function immediately to get global is called immediately by adding () after the function

let global = function(){
    return this} ()Copy the code

In cases where a function does not specify this, this is equal to the Global object.

Math

You see the word, it’s an object that provides some mathematical processing. Computations provided on Math are faster than computations performed directly on JavaScript.

Object properties

Can be used as needed


Math.E   // The base e of the natural logarithm is 2.718281828459045
Math.LN10  // Natural logarithm base 10 2.302585092994046
Math.LN2    // Natural logarithm base 2 0.6931471805599453
Math.LOG2E  // Log base 2 of e
Math.LOG10E  // Log base 10 of e
Math.PI      / / PI.
Math.SQRT1_2  // The square root of 1/2
Math.SQRT2    // Square root of 2
Copy the code

min() max()

Multiple arguments can be passed in

max = Math.max(3.54.17.9)   / / 54
min = Math.min(8.9.0)      / / 0
Copy the code

To find the maximum/minimum value in an array, use the extension operator.

let a = [1.9.6.19.89.3]
Math.max(... a)Copy the code

Rounding method

// round up
Math.ceil(23.4)   / / 24
// round down
Math.floor(23.8)  / / 23
// Round off
Math.round(23.4)   / / 23
Math.round(23.5)   / / 24
// The closest single precision floating-point value is personally useless
Math.fround(23.5)  / / 23.5
Math.fround(23.9)  / / 23.899999618530273
Copy the code

random

Random number: returns a random number in the range 0 to 1. 0,1 contains 0 but does not contain 1

A random number from 1 to 10 is obtained

You can use round up/round down

Math.ceil(Math.random()*10)
Math.floor(Math.random()*10+1) // 0.1 rounded down is 0,9.9 rounded down is 9, so we need to add one
Copy the code

Other methods

Some commonly used

Math.abs(x)  Return the absolute value of x
Math.log(x)  // Return the natural log of x
Math.pow(x,power)  // power of x
Math.sqrt(x)   // return the square root of x
Math.cbrt(x)   // Return the cube root of x
Math.acos(x)   // return arccosine
Math.cos(x)    // return the cosine
Math.sin(x)    // return sine
Math.tan(x)    // return the tangent
Copy the code