Many a folk have questioned “what is this void I see all over the place in As3?’
Well folks, I’ll tell you what this void means and why you should use it.
First, you must understand Functions in AS3 and the parameters and references and so on.
In ActionScript 3.0, all arguments are passed by reference, because all values are stored as objects. However, objects that belong to the primitive data types, which includes Boolean, Number, int, uint, and String, have special operators that make them behave as if they were passed by value.
The void operator evaluates an expression and then discards its value, returning undefined. The void operator is often used in comparisons that use the == operator to test for undefined values.
The void data type contains only one value, undefined. In previous versions of ActionScript, undefined was the default value for instances of the Object class. In ActionScript 3.0, the default value for Object instances is null. If you attempt to assign the value undefined to an instance of the Object class, Flash Player will convert the value to null. You can only assign a value of undefined to variables that are untyped. Untyped variables are variables that either lack any type annotation, or use the asterisk (*) symbol for type annotation. You can use void only as a return type annotation.
If a function does not return a value, then its return type should be void. When a function typed as void is executed, or simply fails to return a value with the return statement, its return value becomes undefined.
So, for example:
If you don’t want your function to return an actual value then the return type should be set to void. A function that has a type set as void or simply fails to return a value with the return statement, its return value becomes undefined.
function nothingHere():void {
// nothing here
}
trace(nothingHere()); // traces undefined
The function nothingHere above returns nothing and also accepts no values since it doesn’t have any parameters. If you try to pass values into the function when it has no parameter list defined for it in ActionScript 3 you will get an error…like this:
nothingHere(3); // This will return an error folks
In AS2, the Void type was used as the parameter list in a function definition to indicate no parameters. In AS3, you just leave the list empty. Using Void in the parameter list in ActionScript 3 will actually make Flash think that a variable named Void is defined for the parameter list of that function.
More sources: http://www.senocular.com/flash/tutorials/as3withflashcs3/
Posted in: Flash Helps | Comments Off