MACROMEDIA FLASH MX 2004-USING ACTIONSCRIPT IN FLASH Use Manual page 42

Using actionscript in flash
Hide thumbs Also See for FLASH MX 2004-USING ACTIONSCRIPT IN FLASH:
Table of Contents

Advertisement

Using strict typing also helps to ensure that you do not attempt to access properties or methods
that are not part of an object's type.
Another advantage of strict data typing is that Flash MX 2004 automatically shows code hints for
built-in objects when they are strictly typed. For more information, see
trigger code hints" on page
Files published using ActionScript 1 do not respect strict data typing assignments at compile
time, so assigning the wrong type of value to a variable that you have strictly typed doesn't
generate a compiler error.
var x:String = "abc"
x = 12 ; // no error in ActionScript 1, type mismatch error in ActionScript 2
The reason for this is that when you publish a file for ActionScript 1, Flash interprets a statement
such as
var x:String = "abc"
doesn't support slash syntax.) This behavior can result in an object that is assigned to a variable of
the wrong type, causing the compiler to let illegal method calls and undefined property references
pass through unreported.
Therefore, if you are implementing strict data typing, make sure you are publishing files for
ActionScript 2.0.
Casting objects
ActionScript 2.0 lets you cast one data type to another. The cast operator that Flash uses takes the
form of a function call and is concurrent with explicit coercion, as specified in the ECMA-262
Edition 4 proposal (see www.mozilla.org/js/language/es4/index.html). Casting lets you assert that
an object is of a certain type so that when type-checking occurs, the compiler treats the object as
having a set of properties that its initial type does not contain. This can be useful, for example,
when iterating over an array of objects that might be of differing types but share a base type.
The syntax for casting is
of
is
. Casting is essentially a function call, and the function call returns
item
type
fails at runtime (in files published for Flash Player 7 or later; files published for Flash Player 6 do
not have runtime support for failed casts). If the cast succeeds, the function call returns the
original object. However, the compiler cannot determine whether a cast will fail at runtime and
won't generate compile-time errors in those cases. The following code shows an example:
function bark(myAnimal:Animal) {
var foo:Dog = Dog(myAnimal);
foo.bark();
}
var curAnimal:Animal = new Dog();
bark(curAnimal); // will work
curAnimal = new Cat();
bark(curAnimal); // won't work
42
Chapter 2: ActionScript Basics
145.
as slash syntax rather than as strict typing. (ActionScript 2.0
, where you want the compiler to behave as if the data type
type(item)
"Strictly typing objects to
if the cast
null

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the FLASH MX 2004-USING ACTIONSCRIPT IN FLASH and is the answer not in the manual?

Questions and answers

This manual is also suitable for:

Flash mx 2004 - actionscript

Table of Contents