JavaScript eval function

Defined: eval function computes a string, and executes the JavaScript code
Syntax: eval (string): string is necessary to calculate the string, which contains the statement or expression to be evaluated to execute JavaScript, returns value by calculating the string
Precautions: eval covering property or eval () methods of imparting other properties, and call it the property, the ECMAScript implementation allows a throw exception EvalError

The above is the explanation of the eval function in W3School. Before, I used eval mainly to convert the json string into a js object after obtaining the data transmitted from the background to the foreground. Later, I used JSON.parse (JSON object parsing) and never touched it again. eval did not go deep into the principle and function of eval. When learning execution context yesterday , I found a different side of eval in Uncle Tom ’s blog.

Eval has a concept: call context. It uses the variable that calls it to act on the environment. If a function defines a local variable x and then calls eval("x"), it will return the value of the local variable. If you call eval("x=1"), it will To change the value of a local variable, it can also be used to declare a new local variable and declare a local function.

Eval is not inside the function. After the context is called, the variable is placed in the global context;
eval is inside the function. After the context is called, the variable is placed in the local context, and this local context will be destroyed as the function exits.

Guess you like

Origin blog.csdn.net/zn740395858/article/details/70312259