|
-
[RESOLVED] Class Default Parameter as a function?
I've been looking through some of the livedoc pages and blogs online, but I can't seem to get an answer to this question (or I'm missing it).
Is it possible to set up a Class to have a Default Parameter as a new function? hopefully that makes sense.
An example of Default Parameters:
Code:
function sayHello(somebody:String = “world”):void {
trace(”hello, “+somebody);
}
If so, what is the syntax for it?
-
I just tried all the ways I could think of to do this, and came up with nothing.
You could have a default value of null and check for it then use the one you wanted to pass.
Code:
package
{
import flash.display.Sprite;
import flash.events.Event;
/**
* ...
* @author ...
*/
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
exec();
exec(function():void{ trace("passed in") } );
}
private function defaultf():void {
trace("defaultf");
}
private function exec(f:Function = null ):void {
if (f != null){
f();
}else {
defaultf();
}
}
}
}
output:
-
Bald By Choice
5tons is a god among mortals.
-
Thanks.
You seem to always give good suggestions for my non-standard questions. at least I think they are non-standard...
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|