Analysis of eval function usage in JavaScript
This article mainly introduces the Eval function usage in JavaScript, the example analyzes the usage technique of eval function in JavaScript, very practical value, the friend who needs can refer to the
This example analyzes the usage of eval functions in JavaScript. Share to everyone for your reference. The specific analysis is as follows:
Eval () has only one parameter, and if the incoming argument is not a string, it returns this argument directly. Otherwise, the string is compiled as a JS code, and a syntax error (SYNTAXERROR) exception is thrown if the compilation fails. If the compilation succeeds, the code is executed, returns the value of the last expression or statement in the string, and returns undefined if the last expression or statement has no value. If the string throws an exception, the exception passes the call to Eval ();
The most important thing about Eval () is that it uses the scope environment of the variable that invokes it, that is, it looks for the value of the variable and the code that defines the action and the local scope of the new variable and function exactly the same.
?
1 2 3 4 5 |
Eval ("var x = 100"); Eval ("var y = 11"); Console.log (x * y); X * y = = 1100 eval ("function foo (x) {return Math.pow (x,x);}"); Console.log (foo (5)); 25 |
The context of the Eval string execution is the same as the context of the calling function, which does not make it run as part of the function:
?
1 2 3 4 |
var foo = function (a) {eval (a);}; Foo ("return;"); |
The code above is global to the context in which eval (a) is being executed, and using return in the global context throws a syntax error: The return not in function.
Eval () has the ability to modify local variables, which is a big problem for the JS optimizer. To simplify the JS interpreter, the ECMASCRIPT3 standard stipulates that no interpreter is allowed to alias Eval (), and if the eval () function throws a Evalerror exception through an alias call.
In fact, most implementations are not. When invoked through an alias, Eval () executes its string as the top-level global code. The execution code may define new global and global functions, or assign values to global variables, but cannot use or modify local variables in the calling function, so it does not affect code optimization within the function.
In ECMAScript5, attitudes are different: objections to throwing evalerror anomalies. When calling the Eval () function directly using an unqualified name in ECMAScript5, it is often called direct eval, and when the eval () is invoked directly, it is always executed within the context scope in which it is invoked. Other indirect calls use global objects as their context scopes and cannot read and write and define local variables and functions. (But actually I found in the Firebug test that all of them were modified by global variables:()
A scenario that requires a real eval to execute a snippet is rare and may be more likely to use the global eval rather than the local eval.
Earlier versions of IE prior to IE9 are not global eval when invoking Eval () via aliases, but IE defines a execscript () global function to complete the global eval function (single-core eval () is slightly different and execscript () always returns null).
ECMAScript5 Strict mode imposes more restrictions on the Eval function behavior. When using eval or eval execution code in strict mode at the start of the "use strict" instruction, Eval is a local eval in the private context. In addition, the strict mode lists the eval as reserved words, which makes eval () more like an operator and cannot overwrite the eval () function with an alias , and the variable name, function name, function parameter, or parameter of the exception capture cannot be named "eval".
I hope this article will help you with your JavaScript programming.