Técnico

Variable Scope in ActionScrip 3

Do you know what happens in the follow code?

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.

Autor(a)

Bruno Sales

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *