Eval () function usage detailed

Source: Internet
Author: User

An explanation of the eval () function usage:
This function may not be used too often, but in some cases it is very useful, and the use of the Eval () function is described below.
Syntax structure:

eval (str)

This function can accept a string str as a parameter, and this str as a piece of JavaScript code to execute, if str execution result is a value return this value, otherwise return undefined. If the argument is not a string, the parameter is returned directly, as the instance reads:

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

In the above code, it is particularly important to note that the last statement declares an object, and if you want to return this object, you need to nest a layer of parentheses outside the object, as follows:

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

The above content simply introduces the use of the Eval () function, which is relatively easy to understand. The most confusing thing about this function is the scope of the problem, the following examples to introduce the relevant content, first look at a code example:

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

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

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

In the above code, two alert () statements can pop up 1 normally. However, this is possible in a standard browser, but in IE8 and IE8 the following browsers still behave like eval (), scoped to the scope in which they are located. This time you can use the Internet Explorer unique Window.execscript () to solve the IE8 and IE8 browser problems. 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 (); Console.log (x);

If the browser supports Window.execscript (), use this function, Window.eval () is not supported, so you can resolve problems IE8 and IE8 the following browsers.
Special Note: All of the above codes are recommended to be copied locally for testing and may be incorrect in this editor.

Eval () function usage detailed

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.