MACROMEDIA FLEX 2-MIGRATING APPLICATIONS TO FLEX 2 Bedienungsanleitung Seite 31

  • Herunterladen
  • Zu meinen Handbüchern hinzufügen
  • Drucken
  • Seite
    / 184
  • Inhaltsverzeichnis
  • LESEZEICHEN
  • Bewertet. / 5. Basierend auf Kundenbewertungen
Seitenansicht 30
Initializing variables 31
About undefined
In general, ActionScript 2.0 allowed accessing undeclared variables whose value had not yet
been set. By default, their value was
undefined. In ActionScript 3.0, accessing undeclared
variables results in a ReferenceError. You can use the
hasOwnProperty() method to check if a
variable has been declared:
if (hasOwnProperty('b') == false)
b = 20;
if (someObj.hasOwnProperty('myDynamicProp') == false)
someObj.myDynamicProp = 22;
Alternately, you can use a try/catch block to catch the ReferenceError; for example:
try {
bb;
} catch(x:ReferenceError) {
print("no bb");
bb = 20;
}
Another alternative is to check if the typeof the variable is undefined.
if (typeof bb == undefined)
bb = 22;
The typeof keyword does not throw a ReferenceError in this case, but rather returns
undefined.
For a dynamic class such as Object, the compiler expects that instances will often have
dynamic properties added to them. The compiler does not throw a ReferenceError when
accessing an undeclared property of a variable if its value is an instance of a dynamic class; for
example:
var a:Object = new Object();
if (a.foo == undefined) // Does not throw a ReferenceError.
a.foo = 22;
About NaN
NaN (Not a Number) is a special instance of Number used to indicate a value which is outside
the range of valid Numbers. Any Boolean valued comparison involving
NaN (such as ==, ===,
=>, and >) always returns false because any Number operation involving NaN is bogus. This
is exactly how C++, Java, and other languages treat
NaN as well.
For comparisons with NaN, use the global isNaN() method:
var a:Number;
if (isNaN(a))
a = 22;
Seitenansicht 30
1 2 ... 26 27 28 29 30 31 32 33 34 35 36 ... 183 184

Kommentare zu diesen Handbüchern

Keine Kommentare