I’m running a simple test and i get a strange behaviour, here is the js code:
var handler = function(index,params){
params.id = index;
}
function test(){
var params = {'id':0};
console.log(params);
gradualRepetitionCalls(0,10,params);
}
function gradualRepetitionCalls(index, maxIndex,params)
{
if (index < maxIndex)
{
handler(index,params);
index++;
gradualRepetitionCalls(index,maxIndex,params);
}
}
test();
The strange thing is that the console.log(params) shows that the id is ‘9’ while i would expect it to be ‘0’. Is console.log() asynchronous?
Well, your code snippet is overly complex, try this:
In Firefox it shows:
but when I click on an object to drill down, it show
id=1. In Google Chrome it just showsObject, butid=1when drilled down.The reason for this odd behaviour is that
consoleunderstand that you are logging an object (not just a string) and every time you look at the console or refresh it, it display the current state of that object.If you find this behaviour error-prone and counter-intuitive, here are some workarounds:
Also note that this is how JavaScript debuggers work, it’s not JavaScript’s fault per se.