Eval () function usage in JavaScript _ javascript skills

Source: Internet
Author: User
The eval () function computes the JavaScript string and executes it as the script code. This article will show you how to calculate the JavaScript string for eval () function and execute it as the script code.

If the parameter is an expression, the eval () function executes the expression. If the parameter is a Javascript statement, eval () executes the Javascript statement.

Syntax

The code is as follows:


Eval (string)

Parameters Description
String Required. The string to be calculated, which contains the JavaScript expression to be calculated or the statement to be executed.

Eval () function usage details:

This function may not be used too frequently, but it may play a very important role in some cases. The following describes the eval () function usage.

This function can take a string str as a parameter and execute it as a javascript code. if the str execution result is a value, this value is returned; otherwise, undefined is returned. If the parameter is not a string, this parameter is directly returned. The example is as follows:

Eval ("var a = 1"); // declare a variable a and assign a value of 1. Eval ("2 + 3"); // executes the ADD operation and returns the operation value. Eval ("mytest ()"); // execute the mytest () function. Eval ("{B: 2}"); // declares an object.

In the above code, note that the last statement declares an object. to return this object, you need to nest a parentheses outside the object, as shown below:

The code is as follows:


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

The above briefly introduces the eval () function usage, which is easier to understand. The most confusing thing about this function is its scope. next we will introduce the relevant content in conjunction with the instance. let's take a look at the code example:

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

In the code above, the First alert () function can bring up 1, and the second will report an error because x is undefined.
As shown in the preceding figure, the eval () function does not create a new scope, and its scope is its scope. This is true for all mainstream browsers, but sometimes the eval () function scope needs to be set to global. of course, eval () can be used in the global scope, but it is often used in actual applications, you need to use this function with a global scope in the local scope. in this case, you can use window. for example, the above code can be transformed as follows:

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

In the above code, both alert () statements can pop up normally. However, this method can be used in standard browsers. However, in IE8 and IE8 browsers, the performance is still the same as in eval (). The scope is the scope where they are located. In this case, you can use window.exe cScript (), which is unique to the Internet Explorer browser, to solve IE8 and Internet Explorer 8 problems. To ensure compatibility with all mainstream browsers, the code transformation is 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.exe cScript (), use this function. if not, use window. eval (). This will solve the problem of IE8 and IE8 browsers.

The above is a detailed explanation of eval () function usage in JavaScript, which I hope everyone will like.

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.