dimanche 31 juillet 2016

Why does my for loop variable stays in scope? [duplicate]

This question already has an answer here:

This took me a while to track it down, and I can fix it by changing:

for(i = 1; i < 2; i++) 

to

for(var i = 1; i < 2; i++)

But why is it in scope at all in the first example, it must be by design.

loop(); 

function whyIsIinScope()
{
  alert('why is i in scope? i is ' + i ); 
}

function loop()
{
  for(i =1; i < 2; i++)
  {
    whyIsIinScope();
  }
}

https://jsfiddle.net/gbsjv4re/

Aucun commentaire:

Enregistrer un commentaire