Examples:
if (foo) {}
if (foo != undefined) {}
if (foo != null) {}
try {foo} catch(e:Error) {}
Gives a compiler error. How do I prevent this?
Answer (thanks Poke):
// declare the variable first
var foo:DisplayObject;
if (foo == null) {
trace('foo is null') // traces
}
ActionScript 3 is statically typed. That means that every variable has to be declared before it can be used in any way. Declaring a variable is unrelated to its initialization in which the variable gets a value for the first time; very often, both are done at the same time though doing something like
var myVar:uint = 2(this declares the variablemyVarasuintand initializes it with the value2).So in your case, you get an error because you are accessing a variable that has not been declared, so the compiler does not know about the name you are trying to access. So you will need to make sure that your variable is actually declared before you use it.