A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: [RESOLVED] Class Default Parameter as a function?

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    18

    resolved [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?

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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:
    Code:
    defaultf
    passed in

  3. #3
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205
    5tons is a god among mortals.
    Follow me on Twitter: http://twitter.com/jasondefra

  4. #4
    Junior Member
    Join Date
    Jun 2009
    Posts
    18
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center