Differences between eval and window. Eval

Source: Internet
Author: User

Are there any differences between them?

In the development process, it seems that few people add an extra window. For example, the callback function parses strings in JSON format during Ajax.CopyCodeThe Code is as follows :...
Function callback (STR ){
VaR JSON = eval ('+ STR + ')');
}
...

Eval is usually used directly, rather than VaR JSON = Window. eval ('+ STR + ')');
For example, when alert is used for debugging, few users use window. Alert; in IE, they get the event object and use event. Few users use window. event. (In Firefox, event is also supported in some cases, but window. event is not supported. If you are interested, take a look at the whole family who obtained the event)

However, due to implementation differences between various engines, there are still some differences between them.Copy codeCode: var x = 5;
Function FN (){
VaR x = 'jack ';
Eval ('X = 10 ;');
}
FN ();
Alert (x); // --> 5

5 is output in all browsers. This indicates that after FN is called, Eval modifies the local variable X in FN, rather than the global variable X. That is, the closure environment of eval execution is in fn.

Change the eval of the above Code to window. Eval. After testing, we find that the performance of each browser is different.

IE6/7/8: 5 is output, that is, the global variable X is not modified, and the local variable X is modified.
Ie9/Firefox/Safari/Chrome/Opera: Output 10. The global variable X is modified.

We can draw a conclusion.
In IE6/7/8, Eval is the same as window. Eval. It is written in a UDF as a local closure; otherwise, it is a global closure.
In ie9/Firefox/Safari/Chrome/opera, Eval is the same as IE6/7/8, and window. Eval is a global Closure even if it is written in a custom function.

In addition, windows. .exe cscript in ieis always executed under global closures. It is surprising that Chrome also supports this method. Oh, Chrome is a standard, and IE is indispensable.

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.