
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Datatypes and Type Checking
|
29
And you could access a nonexistent method or property without errors:
// ActionScript 1.0 code...
var s = new Sound( );
s.thisMethodDoesntExist( ); // No error in ActionScript 1.0!
As we learned earlier, the preceding code—without any modifications—would not
cause errors even in ActionScript 2.0! That’s because ActionScript 2.0’s type checking
is an opt-in system. In order to activate type checking, you must specify the datatype
of the variable, property, parameter, or return value being used. Hence, to cause the
preceding ActionScript 1.0 code to generate helpful errors in ActionScript 2.0, we
declare the variables’ datatypes, as follows:
// ActionScript 2.0 code...
var x:Number = 10; // Here, x's datatype starts as Number.
x = "hello world"; // This attempt to store a string in x causes an error!
var s:Sound = new Sound( );
s.thisMethodDoesntExist( ); // ERROR! No such method...
// Here's the output:
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 3: Type mismatch in
assignment statement: found String where Number is required.
x = "hello world"; // This attempt to store a string in x causes an error!
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 6: There is no method
with the name 'thisMethodDoesntExist'.
s.thisMethodDoesntExist( ); // ERROR! No ...