Eval functions in JavaScript _ javascript skills

Source: Internet
Author: User
This article mainly introduces eval functions in JavaScript. if you need eval functions, you can refer to the code you read today and have encountered an eval function problem. now, I have read many blog posts, I still don't know eval functions very well. there is a code that I can't understand all the time:

/* Var start = [], end = [], timings = []; */function f () {// simulation program execution time var sum = 0; for (var I = 0; I <100000; I ++) {sum = sum/(I + 1) ;}} function repeat (n, action) {for (var I = 0; I
 
  

If I move the local variables in the above benchmark to the global, everything is normal.

Why does the eval function produce this effect? Is it equivalent to alias the eval function?

When eval () is called directly, it is always executed within the context scope of calling it. that is to say, it can access the variables in the repeat function and cannot access the variables in the benchmark function, however, the global variables can be accessed in the function. Therefore, after you set the start variables to global variables, you can return the expected results.

Function repeat (n, action) {for (var I = 0; I
   
    

The start and end variables cannot be accessed in repeat.

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.