The eval () function in JavaScript is detailed _ basics

Source: Internet
Author: User
Tags define local eval variable scope

Eval ("1+2"),-> 3

The dynamic judgment of strings in source code is a very powerful language feature that is hardly necessary to be applied in practice. If you use eval (), you should carefully consider whether you really need to use it.

One, eval () is a function or an operator

Eval () is a function, but because it has been treated as an operator ... Earlier versions of the JavaScript language defined the Eval function, and modern JavaScript interpreters performed a lot of code analysis and optimization. The problem with Eval is that the code used for dynamic execution is usually not parsed, in other words, if a function calls Eval, then the interpreter will not be able to further optimize the function, and another problem with eval defined as a function is that it can be given other names, Var f=eval , then the interpreter cannot be assured of optimizing any function that invokes F (). And when Eval is an operator, these problems can be avoided.

Ii. eval ()

Eval () has only one parameter. If the incoming argument is not a string, it returns the function directly. If the argument is a string, it compiles the string as JavaScript code, if the compiler loser throws a syntax error exception. If the compilation succeeds, the code is executed and the last expression in the string is returned, or the value of the statement, if the last expression or statement has no value, eventually returns undefined. If the string throws an exception, the exception passes the call to Eval ().

The most important thing about Eval is that it uses the variable scope environment that invokes it. That is, it looks for the value of a variable and the actions that define the new variables and functions exactly the same as the 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, and similarly, a function can declare a local variable by using the following code:

Eval ("function f () {return x+1;}");

If you call eval in the topmost code, of course, it does it for global variables and global functions.

It is important to note that the string passed to Eval must be in the grammar and cannot be pasted into the function by any of the code fragments, such as Eval ("return;"). is meaningless because return only works in a function, and in fact, 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. If the string is semantically as a separate script, passing it to the eval as a parameter is completely fine, otherwise eval throws a syntax error exception.

Third, global eval ()

Eval () has the ability to change layout variables, which is a big problem for the JavaScript optimizer. However, as a stopgap, the JavaScript interpreter does not have much optimization for functions that call eval. But how does the JavaScript interpreter work when a script defines an alias for Eval and invokes it with another name? In order to simplify the implementation of the JavaScript interpreter, the ECMASCRIPT3 standard stipulates that no interpreter is allowed to give an alias to the Eval. If the Eval function is invoked by an alias, a Eavlerror exception is thrown.

In fact, most implementations do not do that. When invoked through an alias, Eval executes its string as the top-level global code. The code that executes 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 this does not affect code optimization within the function.

ECMAScript5 is opposed to the use of eavlerror, and regulates the behavior of eval, "direct eval", which is often referred to as "direct eval" when the Eval () function is invoked directly using an unqualified "eval" name. When you call Eval () 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, write, and define local variables and functions. Here's a sample code:

Copy Code code as follows:

var geval=eval; Calling Evla with aliases will be the global eval
var x= "Global", y= "global"; Two Global variables
function f () {//functions execute local eval
var x= "local"; Define Local variables
Eval ("x + + ' chenged ';"); /Direct use of the values of local variables changed by eval
return x; Returns the changed local variable
}
The global eval is performed within the function g () {//.
var y= "local";
Geval ("Y + = ' changed ';"); The direct call changed the value of the global variable
return y;
}
Console.log (f (), x); Change layout changed, output "local changed global"
Console.log (g (), y); Changed global variable, output "local global Changed"

This global eval behavior is not just a compromise of the need for code optimization, it is actually a very useful feature that allows us to execute a global script snippet that has no dependencies on the context. There are few scenarios where you really need eval to execute code snippets. But when you really realize the need for it, you are more likely to use the global eval rather than the local eval.

Four, strict eval ()

ECMAScript5 Strict mode imposes more restrictions on the behavior of the Eval () function and even imposes restrictions on the use of the identifier eval. When an eval is lowered in strict mode, or the code snippet executed by eval starts with the "use strict" instruction, the eval here is the local eval in the private context. That is, in strict mode, an eval-executed snippet can query or change a local variable, but it cannot define a new variable or function in a local scope.

In addition, the strict mode lists "eval" as a reserved word, which makes eval () more like an operator. You cannot overwrite the eval () function with an alias. and the variable name, the function name. Neither the function argument nor the parameter of the exception capture can be named Eval.

Bao Jianfeng from sharpening out, plum blossom incense from the bitter cold.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.