The Type checking can occur in time of compilation or time of execution. The languages static Type , such as C++ and Java, they make the verification in compilation time. As a language dynamically Type , the Action Script 3,0 verifies in runtime. Linguages dynamically type offer to flexibility when structuralizing your code. For example, the following code not specifies the data type of the parameter “xParam”. In runtime, the parameter is used for to get the value of the type Number and one value of the type String. In function dynamicTest() is used the operator is for to test if the parameter is of the type Number our String
[as]package {
import flash.util.trace;
import flash.display.Sprite;
public class Checktype extends Sprite {
public function Checktype(){
this.dynamicTest(100);
this.dynamicTest(“uma cadeia de caracteres”);
}
public function dynamicTest(xParam):void{
if(xParam is String){
var myStr:String = xParam;
trace(“String: “+myStr);
}else if(xParam is Number){
var myNum:Number = xParam;
trace(“Number: “+myNum);
}
}
}
}[/as]
Outher example of the offered flexibility offered involves the use of the properties and methods that they are not known in compilation time. The compiler of the ActionScript 3,0 allows that you he uses properties and he calls methods that do not exist in compilation time. This allows that you it creates properties dynamically or it attributes methods in runtime. For example, the following code creates a called function runtimeTest () that it invokes a method and it returns a property, none known the compiler. The code will not generate an error of the compilation time, but if the property or the method will not be accessible in execution time, an error in execution time will occur.
[as]function runtimeTest(xParam){
xParam.someMethod();
return xParam.someProperty;
}[/as]
to declare a data type for an variable, it uses the attribute var when declaring the variable and adds the operator colon (“:”) followed for the type of data as a suffix to the changeable name. To associate a data type with a parameter, you the operator uses colon (“:”) followed for the data type. For the example, the following code adds the information of the data type to the parameter of xParam, and declares it one myParam changeable with an explicit data type:
[as]function runtimeTest(xParam:String) {
trace(xParam);
}
var myParam:String = “hello”;
runtimeTest(myParam);
[/as]
More:
Type checking Action Script 3.0
Para saber mais:
Adobe Labs
Thanks for this post 🙂