Home >Web Front-end >JS Tutorial >Analysis on the usage of eval function in javascript_javascript skills

Analysis on the usage of eval function in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:02:191353browse

This article analyzes the usage of eval function in JavaScript with examples. Share it with everyone for your reference. The specific analysis is as follows:

eval() has only one parameter. If the parameter passed in is not a string, this parameter will be returned directly. Otherwise, the string will be compiled as js code. If the compilation fails, a syntax error (SyntaxError) exception will be thrown. If the compilation is successful, the 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 be returned. If the string throws an exception, the 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, its operations of finding the value of a variable and defining new variables and functions are exactly the same as the code in the local scope.

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 in which the eval string is executed is the same as the context in which the function is called, which does not allow it to be run as part of the function:

var foo = function(a){
  eval(a);
  };
foo("return;");

Because the context in which eval(a) is executed in the above code is global, using return in the global context will throw a syntax error: return not in function.

eval() has the ability to modify local variables, which is a big problem for the js optimizer. In order to simplify the implementation of the js interpreter, the ECMAScript3 standard stipulates that any interpreter is not allowed to assign aliases to eval(). If the eval() function is called through an alias, an EvalError exception will be thrown.
In fact, most implementations are not like this. When called through an alias, eval() will execute its string as top-level global code. Executing 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 main calling function, so it will not affect the code optimization within the function.

In ECMAScript5, the attitude is different: it is against throwing EvalError exception. In ECMAScript5, when the eval() function is called directly using an unqualified name, it is usually called "direct eval (direct eval)"; when eval() is called directly, it is always executed within the context scope in which it is called. Other indirect calls use the global object as their context scope and cannot read, write and define local variables and functions. (But actually I found in the firebug test that global variables were modified :( )

There are very few scenarios where real eval is needed to execute a code segment. Global eval may be used more often than local eval.

Early versions of IE before IE9 were not global eval when calling eval() through an alias, but IE defined a global function of execScript() to complete the function of global eval (single-core eval() is slightly different, execScript () always returns null).

ECMAScript5 strict mode imposes more restrictions on eval function behavior. When executing code using eval or eval in strict mode starting with the "use strict" directive, eval is a local eval in a private context. In addition, 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, function parameters, or exception capture parameters cannot be named "eval".

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn