In this paper, the discovery of an appropriate title is not so easy, huh, huh, so in this note under the first two purposes of this article:
(1) introduction of the eval function in javascript usage
(2) how to function within the implementation of the overall code
► eval first term of usage, the content is relatively simple, you can skip familiar.
eval function receives a parameter s, if s is not a string, then directly back s. Otherwise, the implementation of s statement. If the statement s results of the implementation of a value, then return this value, otherwise return undefined.
Are required to pay special attention to the grammar object statement "()" and should not return a value, necessary brackets would return the value of easy examples are 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 statements, the only implementation, and should not return values. Commonly used in order to return "()" This object declaration statements must be enclosed in quotation marks to be converted to regular expressions in order to return its value. This is also the use of JSON for Ajax to develop one of the basic principles. In the example we can clearly see, the second alert output statements are undefined, and a third one added in parentheses after the output statement are the subject of express.
► For the time being the focus of this article, how to function within the implementation of the overall 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 is equivalent to demo1 function: function demo1 () (var s = 'local';), which defines a local variable s.
Finally the output is so global is nothing strange things, after all, everyone is very clear the distinction between local variables and global variables.
Experience careful look, we find that the characteristics of eval function, which always call it in the context of the variable space (also known as: Bao, closure) to implement, regardless of the definition are variable or function definition are so, so the following code will produce undefined function 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 function defined in the local space, demo2 function can access that on the visit to see the outside.
Ajax in the actual development, and sometimes we need dynamic access to code from the server to perform in order to alleviate a problem loading the code too much, or are some of the code are self-generated through Javascript, I hope to use eval function to its implementation.
But this kind of dynamic access to code, job function normally completed, such as:
function loadCode () (
var code = getCode ();
eval (code);
)
Eval can not be visible in the overall implementation of space, which give the development will cause a lot of questions, but also seen a lot of people that end郁闷.
But even now finally found a solution,嘿嘿, can be simultaneously compatible with 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 in the function definition of the overall code, you can by calling X2.Eval (code) method, an example is as follows:
var s = 'global';
function demo3 () (
X2.Eval ( 'var s = "local"');
)
demo3 ();
alert (s); //->' local '
Can be seen in demo3 function redefines the global variable s = "local".
Attention is required X2.Eval does not return value, if the expression to be evaluated, or use the eval function system. X2.Eval designed to do only with the definition of the overall code.
In fact, see here, perhaps someone feels too easy to solve problem of the point, huh, huh, but it is found that this approach required some luck and skills:
(1) For IE browser, the default has already provided such a function: execScript, for the overall implementation of the code space, but not many people know.
(2) for the Firefox browser, a direct call to eval function, then the caller's space implementation; window.eval If you call in the implementation of the overall space. This person is estimated to know even less. After all, alert (eval == window.eval) return true!
Firefox characteristics of the eval function is very strange, but javascript specification was also able to find its 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.
Meaning that is perhaps the implementation of eval function and the caller are related, but did not say the context of its implementation problems. IE and Firefox so it charge hard to say, we all know good solution.
firefox under eval particularly poor implementation of performance that can be used in place of Function








Responses to “Depth understanding of the eval function in javascript”