The difference between Eval and window.eval

Source: Internet
Author: User

Evalwith theWindow.evalthe Difference

Are there any differences between them?

Few people seem to add an extra windowin the development processand feel superfluous. such as The callback function in the Ajax process to parse the JSON format string

1 ... 2 function Callback (str) {3    var json = eval (' (' + str + ') '); 4 }5 ...

but because of the different engine implementation differences, they are different. Usually used directlyEval, rather thanvar json = window.eval (' (' + str + ') ');
Or, for example, when debuggingAlert, few people useWindow.alert;IEGets the event object using theEvent, few people usewindow.event. (Firefoxalso support in some casesEventand not supportwindow.event. )

  1  var x = 5;    3     5   var x = ' Jack ';    7   eval (' x=10; ');    9  }  10  11  fn ();  12  13  alert (x); -->5 

modified, put the above code Eval Replace Window.eval , the tests found that the performance was different in each browser. all the browsers output is 5, stating that after calling fn , eval modifies the fn The local variable xinside, not the global x. the closure environment that Eval executes is within FN.


IE6/7/8:still output5, that is, the global variable is not modifiedx, the modification is still a local variablex.
Ie9/firefox/safari/chrome/opera:OutputTen, the global variable is modifiedx.

Can come to a conclusion
in Ie6/7/8,likeeval and window.eval , writing within a custom function is a local closure, otherwise it is a global closure.
Ie9/firefox/safari/chrome/opera ,eval with the above ie6/7/8, Window.eval is a global closure even if it is written inside a custom function.

The difference between Eval and window.eval

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.