eval(“1 2”),-> 3
Dynamically determining strings in source code is a very powerful language feature that is almost unnecessary to apply in practice. If you use eval(), you should carefully consider whether you really need to use it.
1. Is eval() a function or an operator
eval() is a function, but it has been treated as an operator. . Early versions of the JavaScript language defined the eval function, and modern JavaScript interpreters perform extensive code analysis and optimization. The problem with eval is that code used for dynamic execution generally cannot be analyzed. In other words, if a function calls eval, the interpreter will not be able to further optimize the function and define eval as another part of the function. The problem is that it can be given another name, var f=eval; then the interpreter cannot safely optimize any function that calls f(). When eval is an operator, these problems can be avoided.
2. eval()
eval() has only one parameter. If the parameter passed in is not a string, it returns this function directly. If the parameter is a string, it will compile the string as JavaScript code, and throw a syntax error exception if the compilation fails. If the compilation is successful, this piece of code will be executed and the value of the last expression or statement in the string will be returned. If the last expression or statement has no value, undefined will eventually be returned. If the string throws an exception, this exception will pass the call to eval().
The most important thing about eval is that it uses the variable scope environment in which it is called. That is, it looks up the values of variables and defines new variables and functions exactly the same as code in the local scope. If a function defines a local variable x and then calls eval("x"), it returns the value of the local variable. If it calls eval("x=1"), it changes the value of the local variable. If the function calls eval("var y=2;"), it declares a new local variable y. Similarly, a function can declare a local variable through the following code:
eval(“function f(){return x 1;}”);
If eval is called in the top-level code, of course, it will act on global variables and global functions.
It should be noted that the string passed to eval must be grammatically consistent. You cannot paste arbitrary code snippets into the function through eval. For example: eval("return;") is meaningless, because return only It only plays a role in the function, and in fact, the context when the eval string is executed is the same as the context in which the function is called, which does not allow it to run as part of the function. If the string is semantic as a separate script, then there is no problem passing it to eval as a parameter, otherwise, eval will throw a syntax error exception.
3. Global eval()
eval() has the ability to change layout variables, which is a big problem for JavaScript optimizers. However, as a stopgap measure, the JavaScript interpreter does not do much optimization for functions that call eval. But how does the JavaScript interpreter work when a script defines an alias for eval and calls it under another name? In order to simplify the implementation of JavaScript interpreters, the ECMAScript3 standard stipulates that no interpreter is allowed to assign aliases to eval. If the eval function is called through an alias, an EavlError exception will be thrown.
Actually, most implementations do not do this. When called through an alias, eval will execute its string as top-level global code. The executed code may define new global variables and global functions, or assign values to global variables, but it cannot use or modify local variables in the calling function. Therefore, this will not affect code optimization within the function.
ECMAScript5 is against the use of EavlError and standardizes the behavior of eval, "direct eval", when the eval() function is called directly using the unqualified "eval" name, it is usually called "direct eval". When eval() is called directly, it is always executed within the scope of the context in which it is called. Other indirect calls use the global object as their context scope and cannot read, write, or define local variables and functions. Here is a sample code:
ECMAScript5 strict mode imposes more restrictions on the behavior of the eval() function and even on the use of the identifier eval. When eval is called in strict mode, or the code segment executed by eval begins with a "Use strict" directive, eval here is a local eval in the private context. That is, in strict mode, the code segment executed by eval can query or change local variables, but cannot define new variables or functions in the local scope. Additionally, strict mode lists "eval" as a reserved word, which makes eval() more like an operator. The eval() function cannot be overridden with an alias. And variable names, function names. Neither function parameters nor exception capture parameters can be named eval.
The edge of a sword comes from sharpening, and the fragrance of plum blossoms comes from the bitter cold.