JavaScript variable scope: Not confined to statement created in?

In Unity’s JavaScript, method-level variables are always available throughout the method. (Skip over code if you know exactly what I mean).

For instance:

function SomeFunction(){  
    for (var i : int = 0; i < foo; i++)
    {
        //declared var in for(){}
	    var TMCBSRef : TMCBS = cell.GetComponent(TMCBS);			
    }  
   //---------------------------------------------------------------
    for (var cell : GameObject in CellsArray)
    {
        //declared var in foreach(){}, "Already Exists" error            
    	var TMCBSRef : TMCBS = cell.GetComponent(TMCBS);	
    }
}

I understand this is intended behavior, but I’m wondering if there’s a keyword I can slap on there to make these vars limit their scope to whatever statement they’re in, a la every other programming language I’m familiar with? Otherwise, how do you guys mitigate the confusion caused by this issue?

Thanks for your time. :slight_smile:

Indeed, it’s intended behavior, and not something that can be changed.

I suspect this is a bug, and in Unity 3.5 release note they mention something that looks like a correction for this (search for “for loop not to reuse an already declared variable”). I have not tested 3.5 yet, and the text in the release note is very obscure, but it seems a reference to this weird behaviour.