Looking at the code today, the question of an eval function, a lot of blogging now, or the Eval function, there's a code that's never been understood:
/* 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<n i++) {
eval (action);//eval function
}
}
function Benchmark () {
var start = []
, end = []
, timings = [];
Repeat (Start.push, "new Date (). GetTime ()); F (); End.push (new Date (). GetTime ())");
for (var i =0; i< start.length; i++) {
timings[i] = end[i]-start[i];
return timings;
}
Benchmark (); The result is: []
//If I move the local variable in the benchmark above to the global, everything is OK.
If I move the local variable in the benchmark above to the global, everything is OK.
Why does the Eval function here have this effect? Is it the equivalent of aliasing the eval function?
When you call Eval () directly, it is always executed within the context scope in which it is invoked, that is, he can access the variables in the repeat function, and not access the variables in the benchmark function, but in the function is a variable that can access the global scope, So when you set the start variables to global, you can return to the desired results.
function repeat (n, action) {for
(var i=0; I<n i++) {
Start.push (new Date (). GetTime ()); F (); End.push (new date (). GetTime ()); eval function
}
}
Start,end variable not accessible in repeat