Home >Web Front-end >JS Tutorial >A brief discussion on the function and scope of eval()

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

零下一度
零下一度Original
2017-06-25 09:22:493289browse

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!

Statement:
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