JavaScript eval function detailed description and compatibility processing

Source: Internet
Author: User
Tags eval function definition object object javascript eval

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

Grammar
eval (string) parameter description
String required. The string to evaluate, which contains the JAVASCRIPT expression to evaluate or the statement to execute.

return value
The value, if any, obtained by calculating the string.

First, the use of eval, the content is relatively simple, familiar can be skipped.
The Eval function receives an argument s, and if S is not a string, it returns directly to S. Otherwise execute the S statement. If the S statement executes the result is a value, this value is returned, otherwise the undefined is returned.
A special note is that the object declaration syntax "{}" does not return a value, which needs to be enclosed in parentheses to return the value, as the simple example reads:

var code1= ' "A" + 2 '; An expression
var code2= ' {a:2} '; Statement
Alert (eval (code1)); -> ' A2 '
Alert (eval (code2)); ->undefined
Alert (eval (' + Code2 + ')); ->[object Object]


Whether this is a variable definition or a function definition, the following code produces an error that is not defined by the function:

var s= ' function test () {return 1;} '; A function definition statement
function Demo2 () {
Eval (s);
}
Demo2 ();
Alert (test ()); ->error:test
is not defined


IE and Firefox can be compatible at the same time, as follows:

var x2={}//my namespace:)
X2. Eval=function (code) {
if (!! (window.attachevent &&!window.opera)) {
Ie
Execscript (code);
}else{
Not IE
Window.eval (code);
}
}

Now, if you want to define global code within a function, you can invoke the X2.eval (code) method by invoking the following example:

var s= ' global ';
function Demo3 () {
X2. Eval (' var s= ' local ');
}
Demo3 ();
alert (s); -> ' Local '


Description
The method accepts only the original string as a parameter, and if the string argument is not the original string, the method returns without any changes. Therefore, do not pass a String object as an argument for the eval () function.

If you attempt to overwrite the Eval attribute or assign the eval () method to another property and call it through this property, the ECMAScript implementation allows a Evalerror exception to be thrown.


IE when Eval encounters function processing


There is no function in the 1:eval, direct execution:
Eval ("Alert (' SS ');"); /all browsers are output correctly
The situation in 2:eval has function,function immediate execution:
Eval (function () {alert (' SS ');}) ();");/ /all browsers correct output
There is a function in the 3:eval, a function reference is saved using a variable, and the function is invoked:
var f=eval ("(function () {alert (' SS ');}");
f ();//ie error: Missing object other browser normal
When the eval defines a function and returns it to the variable, ie complains: missing object. It can be seen that the function defined in the eval under IE does not return to the eval outside successfully.

WORKAROUND: Make the function object return as a result of execution:

Method 1:

var f=eval (() (function () {return function () {alert (' SS ');}}) ()");
f ();//all browsers correct output
Eval calls an immediately executing function that returns a function object after it executes, at which point the reference to that function object is successfully returned to the external variable.

Method 2:

var f=eval ("(false| | function () {alert (' SS ');});
f ();//all browsers successfully output
This method is also the method used in jquery, function as the result of the execution of the expression or return, the same can successfully solve the problem. Of course, the expression is not limited to the above false| | function () {}, which resolves the problem if a function is returned successfully by a variety of expressions:

/* and expression: */
var f=eval ("(True&&function () {alert (' SS ');}");
f ();//all browsers normal output

/* Ternary expression: * *
var f=eval ("(True?function () {alert (' SS ');}: ');");
f ();//all browsers normal output

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.