Eval function Usage Analysis in javascript

Source: Internet
Author: User
Tags define local

Eval function Usage Analysis in javascript

Eval function Usage Analysis in javascript

This article mainly introduces the eval function usage in javascript. The example analyzes the eval function usage skills in javascript, which is very useful. For more information, see

This article analyzes eval function usage in javascript. Share it with you for your reference. The specific analysis is as follows:

Eval () has only one parameter. If the input parameter is not a string, this parameter is directly returned. Otherwise, the string is compiled as js Code. If the compilation fails, a syntax error (SyntaxError) is thrown. If the compilation is successful, run the code and return the value of the last expression or statement in the string. If the last expression or statement has no value, undefined is 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 that calls it, that is, it looks for the value of the variable and defines the operation of the new variable and function, and the code of the local scope is 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 Environment for eval string execution is the same as the Context Environment for calling a function, which cannot be executed as part of the function:

?

1

2

3

4

Var foo = function (){

Eval ();

};

Foo ("return ;");

The above code throws a syntax error: return not in function because the context for executing eval (a) is global.

Eval () has the ability to modify local variables, which is a big problem for the js optimizer. To simplify the implementation of the js interpreter, the ECMAScript3 Standard specifies that no interpreter is allowed to assign an alias to eval (). If the eval () function is called through an alias, an EvalError exception is thrown.

In fact, most implementations are not like this. When called using an alias, eval () treats its string as the top-level global code for execution. Executing the code may define new global variables and global functions, or assign values to global variables, but it cannot use or modify the local variables in the main call function, therefore, code optimization within the function is not affected.

In ECMAScript5, the attitude is different: An EvalError exception is thrown. When ECMAScript5 directly uses a non-qualified name to call the eval () function, it is usually called "direct eval (direct eval)"; When eval () is called directly, it is always executed within the context scope that calls it. Other indirect calls use a global object as its context scope and cannot read/write or define local variables and functions. (But I found in the firebug test that all the global variables have been modified :()

There are few scenarios that require true eval to execute code segments. More scenarios may use global eval instead of local eval.

Earlier versions of IE earlier than IE9 do not call eval () by alias, but IE defines an execScript () to complete the global eval function (single-core eval () is slightly different, execScript () always returns null ).

The strict mode of ECMAScript5 imposes more restrictions on eval function behavior. In strict mode, when eval or eval is used to execute code starting with the "use strict" command, eval is a local eval in the private context. in addition, the strict mode columns eval as reserved words, which makes eval () more like an operator and cannot overwrite eval () functions with an alias, the variable name, function name, function parameter, or exception capture parameter cannot be named "eval ".

I hope this article will help you design javascript programs.

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.