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.