Is there any way that I can reference a var or const as the default value for a function argument in actionscript 3.
I can define default values like null, string, int.
function a( b = null ) { blah... }
But what I want to do is
function a( b = function(){} ) { blah... }
which it seems like there would be a way to do. Presumably through a const
Oddly enough it seems you can't do that, atleast I couldn't get it to work, it won't accept any references to static functions as a default value.
The best I could do was this:
public function myFunction(functionArgument:Function = null):void {
if (functionArgument != null) {
functionArgument();
} else {
defaultFunction();
}
}
As a sidenote I just discovered that you in fact can declare functions like this:
public static const STATICFUNC:Function = function():void { trace("i'm static!") };
But that seems to work the same way as declaring them the sane way, so no luck there either.
Parameter defaults must be compile-time constants, because the compiler has to put in the default value when the code is compiled. This means static functions, no matter how predictable they are, cannot be used as parameter defaults. Compile-time constants are values the compiler knows about by inspecting your code, but not actually running it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With