1 Introduction to the use of the Eval function in JavaScript
(2) How to execute global code within a function
► First, the use of eval, the content is relatively simple, familiar can skip.
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:
Copy Code code as follows:
var code1= ' "A" + 2 '; An expression
var code2= ' {a:2} '; Statement
Alert (eval (code1)); -> ' A2 '
Alert (eval (code2)); ->undefined
Alert (eval (' + Code2 + ')); ->[object Object]
As you can see, for an object declaration statement, it is simply execution and cannot return a value. In order to return an object declaration statement such as the usual "{}", you must enclose it in parentheses to convert it to an expression to return its value. This is one of the fundamentals of using JSON for AJAX development. As you can see clearly in the example, the second alert statement outputs the undefined, and the third with parentheses is the object that the statement represents.
► Now the focus of this article is how to execute global code within a function. To illustrate this issue, let's look at an example:
Copy Code code as follows:
var s= ' global '; Define a global variable
function Demo1 () {
Eval (' var s= ' local ');
}
Demo1 ();
alert (s); ->global
Well understood, the DEMO1 function above is equivalent to: function Demo1 () {var s= ' local ';}, which defines a localized variable s.
So the final output is global is not a strange thing, after all, we can clearly distinguish between local and global variables.
With a closer look, you can see that the eval function is always executed within the context variable space (also known as: Package, closure) that calls it, whether it is a variable definition or a function definition, so the following code produces an error that is undefined by the function:
Copy Code code as follows:
var s= ' function test () {return 1;} '; A function definition statement
function Demo2 () {
Eval (s);
}
Demo2 ();
Alert (test ()); ->error:test is not defined
This is because the test function is defined in the local space and can be accessed within the DEMO2 function and is inaccessible outside.
In actual Ajax development, sometimes we need to dynamically get code from the server to execute to mitigate the problem of loading code too much at a time, or some code is generated through JavaScript itself and wants to be executed using the Eval function.
However, this dynamic acquisition code is generally done within the function, such as:
Copy Code code as follows:
function Loadcode () {
var code=getcode ();
eval (code);
}
It can be seen that eval is impossible to execute in the global space, which brings a lot of problems to development, but also see a lot of people for this depressed.
But now I finally found a solution, hey, can be compatible with IE and Firefox, the following methods:
Copy Code code 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:
Copy Code code as follows:
var s= ' global ';
function Demo3 () {
X2. Eval (' var s= ' local ');
}
Demo3 ();
alert (s); -> ' Local '
Visible, the global variable s= "local" is redefined within the DEMO3 function.
Note that X2.eval does not return a value, and if you want to evaluate the expression, use the system's eval function. X2. Eval is designed to do only global code definitions.
Actually see here, maybe some people feel the problem is too easy to solve the point, hehe, but found that this approach needs some luck and skill:
(1) For IE browser, the default has provided such a function: Execscript, used in the global space to execute code, just know that there are not many people.
(2) for the Firefox browser, call the eval function directly, execute in the caller's space, and execute in the global space if the call Window.eval. The person who knows it is estimated to be less. After all, alert (eval==window.eval) returns to true!
The features of the Firefox eval function are really strange, but they can also be found from JavaScript specifications:
The If value of the Eval property is used into any way other than a direct call (which, other than by the explicit
Name as an Identifier which was the memberexpression in a callexpression), or if the Eval property is assigned to,
A Evalerror exception may is thrown.
This means that the Eval function is executed in relation to the caller, but does not have a problem with its execution context. So IE and Firefox is what is not difficult to say, we know the solution is good.