Home > Web Front-end > JS Tutorial > body text

A brief discussion on the function and scope of eval()

零下一度
Release: 2017-06-25 09:22:49
Original
3211 people have browsed it

The role of eval()

Parses string parameters into js code and runs it, and returns the execution result;

The scope of eval

The scope is valid in all its scope contents

IE8 and below point to window

Solution:

  1. functiona(){

  2. if(window.execScript){ // Supports IE8 and below versions

  3. window.execScript("var x=1");

  4. }

  5. else{ //Commonly used browsers support

  6. ## window.eval(

    "var x=1 ");

  7. }

  8. console.log(x);

  9. }

  10. a();

  11. console.log(x);

Notes

You should avoid using eval, which is unsafe and very performance-consuming (twice, once parsed into a js statement and once executed).

The above is the detailed content of A brief discussion on the function and scope of eval(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!