Variable Scope in ActionScrip 3

Do you know what happens in the follow code?

1
2
3
4
5
6
7
8
9
10
var test:Boolean = false;
if(test)
{
    var name:String;
}
else
{
    name = "Bruno Sales";
}
trace("Name: " + name);

Compile error? Runtime Error? Nothing. The trace result is “Name: Bruno Sales”. The variable was created even if the code inside the IF was never executed.
Thats happens because ActionScript has just 2 types of scope: Global and Local; remembering that Local is everything inside a function.

However, I suggest never using something like this in your development process. Your code logic will be very confusing and will cause problem to maintenance and debug.


No comments yet

Leave a Comment