Window. eval and related methods

Source: Internet
Author: User
This document is backed up from my blog on javaeye.
Updated on April 9, February 27:
Csdn really does. If you get the home page, you can get the home page. If you want to change your title, add "jscript window. Eval ......", It's totally out of the box (but there is no difference between window. eval and eval In JScript ). You can either write "JavaScript" or "JS "! Alas...
The previous post mentioned how to implement eval in global space in functions. Although such a demand is unreasonable in most cases, there are still a few situations where it may be necessary.

JScript has the execScript method that can be used to execute the script. The first parameter is the code string, and the second parameter is the script language. You can select JScript or VBScript.

In other script engines, spidermonkey retains the eval method on the object as early as Js. That is, you can run eval on any object. During execution, this object is added to the scope chain.

For example, {X: 1 }. eval ('x') returns 1, and (o = {X: 2 }). after eval ('var x = 10') O. X is equal to 10.

Basically, O. eval (CODE); similar to with (o) {eval (CODE )}.

The only thing worth noting is a statement like var X. For
VaR o = {X: 1 };
With (o ){
VaR x = 2;
}
Safari (javascriptcore and KJS engine) generates a global x variable. In addition (jscript, spidermonkey, opera, Rhino, etc.), O. X = 10. Note: The prerequisite is that the O has the X attribute.

By using the object. Eval method of spidermonkey, we can know that in Firefox, window. eval () and direct eval () have different effects. The effect of the former is close to that of execScript.

Other engines do not support object. Eval, but opera uses window. Eval as a special case, which has the same effect as Firefox. In addition to Safari, we can also use with (window) {eval (...)} to simulate execScript, but the premise is that you know the names of all var declared on the global, and create these variables on the window in advance-in the case of multiple numbers, this is not practical.

The eval of spidermonkey can also be passed to the second parameter. eval (Code, O) is basically equivalent to O. eval (CODE), but there are still subtle differences between the two. For example
VaR A = 'global ';
VaR code = 'alert ()';
VaR context = (function () {return function (){}})();
Context. eval ('alert ()');
Eval ('alert (a) ', context );
The returned values are different. When eval (Code, O) is used, it will start from O. _ parent _ ([Scope. The name of the function expression, or the with statement outside the function, adds an object to [[Scope.

With regard to the new function (CODE), pies actually uses this method to generate code rather than using eval like some other frameworks. In addition, I also use it to compile the template ". Using a function is much better than eval. Because it is always in a new execution context, it avoids many bugs that may occur in eval (earlier browsers often crash in some special eval ). Therefore, to generate code, use new function first.

However, it is not appropriate for new functions to execute var x = 1 and put X into global. Although this requirement is so rare (that in most cases this requirement is a wrong one), it is skipped because this is the case in this article.

In fact, for spidermonkey and rhino, another method is recommended, that is, the script method. The result of script (CODE) is a function that can be called directly. For example, script ('var A = 1') () generates a variable on global wherever it is executed. You can call script.prototype.exe C to configure the scriptresult. Script.exe C basically corresponds to execScript. Later, the rhinodes are directly called, but cannot be called using script(code=.exe C. Script.exe C on spidermonkeycan contain parameters. If obj is included, the effect is similar to eval (Code, OBJ.

Well, let's summarize:

ExecScript (CODE) on JScript)
Script (CODE) () can be used on spidermonkey and rhino )()
Use window. eval (CODE) in opera)

The only problem is safari. In fact window. Eval has been submitted and fixed as a bug (refer to the http://trac.webkit.org/projects/webkit/changeset/25535), and we hope that the next version of Safari will be able to use window. Eval like opera.

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.