javascript - Variable changed before the fact, can you explain this Chrome V8 behavior? -


i writing javascript program , running in chrome 7, when encountered strange behavior. now, in code, other stuff going on, took me time figure out wasn't me.

i have distilled essence of code below.

<html>  <script>  var data = [1,2,3,4,5];  var data_copy = [];  (var i=0; i<data.length; i++){     data_copy.push(data[i]); }  console.log("printing before:"); console.log(data_copy);  //alert(data_copy);  console.log("------------------------");  (var i=0; i<data_copy.length; i++){     data_copy[i] = data_copy[i] * 1000; }  console.log("printing after:"); console.log(data_copy);  </script>  </html> 

when run on chrome 7, output follows in javascript console:

printing before: [1000, 2000, 3000, 4000, 5000] ------------------------ printing after: [1000, 2000, 3000, 4000, 5000] 

how come first call console.log prints changed version of data_copy?

now, if uncomment "alert" , run same code, expect:

printing before: [1, 2, 3, 4, 5] ------------------------ printing after: [1000, 2000, 3000, 4000, 5000] 

i tried code in node.js , second (normal) output.

any ideas?

is jit optimization gone awry?

or missing obvious?

change console.log(data_copy) console.log(string(data_copy)).

console.log sends object reference chrome's console. alert interrupts script first logged data_copy gets rendered before later modification; without, entire script runs completion before console renders either data_copy reference.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -