The eval () function usage in JavaScript _javascript tips

Source: Internet
Author: User

The eval () function computes the JavaScript string and executes it as a scripting code.

If the argument is an expression, the eval () function executes the expression. If the parameter is a JavaScript statement, Eval () executes the JavaScript statement.

Grammar

Copy Code code as follows:

Eval (String)

Parameters Description
String Necessary. The string to evaluate, which contains the JAVASCRIPT expression to evaluate or the statement to execute.

The eval () functional usage is detailed:

The frequency of this function may not be too high, but in some cases it is very useful, and here's how to use the eval () function.

This function can accept a string of STR as a parameter and execute this str as a section of JavaScript code, or return undefined if the str execution result is a value. If the argument is not a string, the argument is returned directly, as follows:

Eval ("var a=1")//Declare a variable A and assign a value of 1.
eval ("2+3"), perform the addition operation, and return the operation value.
eval ("mytest ()");//execute MyTest () function.
eval ("{b:2}");//Declare an object.

In particular, note that the last statement declares an object, and if you want to return this object, you need to nest a single layer of parentheses outside the object, as follows:

Copy Code code as follows:

Eval ("({b:2})");

The above is a brief introduction to the use of the Eval () function, which is easier to understand. The most puzzling thing about this function is the scope of the problem, the following is an example of the relevant content, first look at a piece of code example:

function A () { 
 eval ("var x=1"); 
 Console.log (x); 
} 
A (); 

In the above code, the first alert () function can pop 1, and the second will error because x is undefined.
As you can see from the above performance, the Eval () function does not create a new scope, and its scope is the scope in which it resides. This is true in all mainstream browsers, but sometimes you need to set the scope of the Eval () function to global, and of course you can use Eval () in a global scope, but often in practice, you need to use this function with global scope in a local scope. This time can be implemented in Window.eval (), such as the above code can be modified as follows:

function A () { 
 window.eval ("var x=1"); 
 Console.log (x); 
} 
A (); 

In the above code, two alert () statements can pop 1 normally. However, this approach is acceptable in standard browsers, but in IE8 and IE8 the following browsers remain the same as the eval () scope is the scope of their. This time you can use the unique window.execscript () of IE browser to solve the problem of IE8 and IE8 browsers. To achieve compatibility with all major browsers, transform the code as follows:

function A () { 
 if (window.execscript) { 
  window.execscript ("var x=1"); 
 } 
 else{ 
  window.eval ("var x=1"); 
 } 
 Console.log (x); 
} 
A (); 

If the browser supports Window.execscript (), this function is not supported, and Window.eval () is used, which resolves the problems of IE8 and IE8 the following browsers.

The above content is small make up to share the JavaScript in the eval () function usage detailed, hope everybody likes.

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.