JavaScript is bound to use of Five eval
- 2021-06-28 10:07:05
- OfStack
eval
eval (parse) parse: With the parameter string inside, we know that executing javascript compiles execution.
Change the value of the global variable:
var x = ; // Defined global variables
alert(x);//
var g = eval("x="); //eval Resolves based on the current context x
alert(x);//
Reference eval in global scope, change the value of global scope, do not change the value of local scope
var g = eval; // Global Reference eval
var x = "global";// Define global variables
(function f() {
var x = "local";
g("x+='changed'");
alert(x);// local variable local
})();//IIFE Execute the expression immediately
alert(x);// Values of global variables globalchanged
Reference within a local scope changes the value of a local variable without changing the value of a global variable
var g = eval; // Global Reference eval
var x = "global";// Define global variables
(function f() {
var x = "local";
eval("x+='changed'");
alert(x);// local variable localchanged
})();//IIFE Execute the expression immediately
alert(x);// Values of global variables global
summary
eval execution will determine whether the changed variable is local or global based on the context, so the key to using the eval function is to see the scope of the reference to eval!