JavaScript Learning (4.9): An Introduction to eval () __javascript

Source: Internet
Author: User
Tags define local
The 4.12 expression evaluates the eval () eval () to interpret a string that consists of JavaScript source code and produces a value. Eval () is a global function.
The 4.12.1 eval () eval () has only one argument, and if the incoming argument is not a string, it returns this argument directly. If the argument is a string, it compiles the string as JavaScript code and throws a syntax error exception if the compilation fails. If the compilation succeeds, the code starts, returns the value of the last expression or statement in the string, and eventually returns undefined if the last expression or statement has no value.      If the string throws an exception, the exception passes the call to Eval (). Eval () uses the scope environment of the variable that invoked it. It looks for the value of a variable exactly as it does the operation of defining new variables and functions and the code in a local scope. For example: Define a local variable x, call eval ("X"), return the value of the local variable, call eval ("X+1"), change the value of the local variable, call eval ("var y = 3;" To declare a new local variable y; eval ("function f () {return x+1;}" Declares a local function, and if you substitute eval () in the top-level code, it is used for global variables and global functions.
* The string passed to eval () must be syntactically---it is meaningless to paste the code fragment into the function by eval (), such as Eval ("return;"), because return is only useful in functions, And 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 (such as x = 0), passing it to Eval () as a parameter is completely fine, otherwise a syntax error exception will be thrown.
var foo =function (a) {eval (a);};
Foo ("return;"); Syntaxerror:return Not in function
Foo ("x= 0;"); Undefined
4.12.2 Global eval ()
The ECMAScript 3 standard stipulates that no interpreter is allowed to give an alias to Eval ().       , if Eval () is invoked through an alias, a Evalerror exception is thrown. ECMAScript 5 opposes the use of evalerror and regulates the behavior of eval (). When you call Eval () directly, it executes 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.
var geval = eval; var x = "Globala", y = "globalb";
function f () {var x = ' Locala ';   Eval ("x + + ' changed ';"); Local eval return x; }
function g () {var y = ' localb ';  Geval ("Y + = ' changed ';"); Global eval return y; }
f (); Localachanged x; Globala g (); Locallb y; Globalbchanged
4.12.3 Strict eval ()
The Ecmasript 5 Strict mode imposes more restrictions on the behavior of Eval (). In strict mode, a code snippet executed with eval (), or eval (), starts with the "use strict" instruction, where Eval () is a local eval in a private context, that is, in strict mode, the code snippet that the eval executes can query or change the local variable, However, you cannot define a new variable or function in a local scope.

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.