Friday, September 3, 2010

In-depth understanding of the eval function in javascript

the discovery of an appropriate title since this is not so easy, huh, huh, so the explanatory notes to this first article of two purposes: (1) describes the eval function in javascript use (2) how to function within the implementation of the Global code? first is the use of eval, the content is relatively simple, familiar can skip. eval function accepts a parameter s, if s not a string, then directly back to s. ..

Found to be an appropriate title since this is not so easy, huh, huh, so in this note under the first two purposes of this article:
(1) describes the eval function in javascript use

(2) how to function in the implementation of the global code


? First is the use of eval, the content is relatively simple, familiar can skip.

eval function accepts a parameter s, if s not a string, then directly back to s. Otherwise the implementation of the s statement. If the results of the implementation s statement is a value, this value is returned, otherwise undefined.

Needs special attention is the object declaration syntax "()" can not return a value, will need brackets return value, a simple example as follows:

var code1 = '"a" + 2'; / / expression

var code2 = '(a: 2)'; / / statement

alert (eval (code1)); //->' a2 '

alert (eval (code2)); / / -> undefined

alert (eval ('(' + code2 +')')); //->[ object Object]

Can see that the object declaration statement, the only implementation, and can not return values. To return to common "()" This object declaration statements must be enclosed in parentheses, in order to convert it to an expression in order to return its value. This is also the use of JSON to be one of the basic principles of Ajax development. In case you can clearly see the second alert statement in the output is undefined, while the third added in parentheses after the statement that the output is the object.

? Now is the focus of this article, how to function in the implementation of the global code. To illustrate this problem, look at an example:

var s = 'global'; / / Define a global variable

function demo1 () (

eval ('var s = "local"');

)

demo1 ();

alert (s); / / -> global

Well understood, the above demo1 function is equivalent to: function demo1 () (var s = 'local';), which defines a local variable s.

So the final output is global is not strange things, after all, everyone is very clear distinction between local variables and global variables.

Taste carefully, you can find the characteristics of eval function, it is always in the context it is called variable space (also known as: bag, closure) within the Executive, whether variable or function definitions are so defined, so the following code will produce function undefined error:

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 functions defined in local space, demo2 functions can be accessed within, not outside on a visit.

While the actual Ajax development, sometimes we need to obtain the code from the server dynamically to implement, Yi Jian Qing code Guoduo a load of, or some code is generated by javascript Zi Shen's, Xi Wang Yong eval Hanshu to its implementation.

But this kind of dynamic access code to complete the work within the function normally, such as:

function loadCode () (

var code = getCode ();

eval (code);

)

Eval can not see the implementation of the global space, which has brought many problems to developers, also saw a lot of people depressed.

But even now finally found a solution, hey, that works with both IE and Firefox, 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 it is to define a function within the overall code, you can call X2.Eval (code) method, an example is as follows:

var s = 'global';

function demo3 () (

X2.Eval ('var s = "local"');

)

demo3 ();

alert (s); //->' local '

Visible within the function in demo3 redefined global variable s = "local".

Note that X2.Eval does not return value, if the expression is evaluated to be carried out, or use the eval function of the system. X2.Eval designed to do only with the definition of the global code.

In fact, that here, perhaps feeling the problem was too easy to solve the point, huh, huh, but found this approach but it's a some luck and skill:

(1) For IE browser, the default has provided such a function: execScript, for the implementation of the code in the global space, but not many people know.

(2) For the Firefox browser, call the eval function directly, then the caller's space implementation; If the call window.eval implementation in the global space. The estimate that even fewer people. After all alert (eval == window.eval) returns true!

Firefox's features eval function is very strange indeed, but javascript specification can be found down the source:

If value of the eval property is used in any way other than a direct call (that is, other than by the explicit use of its

name as an Identifier which is the MemberExpression in a CallExpression), or if the eval property is assigned to,

an EvalError exception may be thrown.

Meant to say about the implementation of eval function and the caller is relevant, but did not say the context of its implementation problems. So IE and Firefox also hard to say right and wrong, like we know the solutions.

No comments:

Post a Comment