views:

32

answers:

2

I have a function, for example

function test(p1:int=7,p2:Boolean=true,p3:uint=0xffff00,p4:Number=55.5) {
//instructions
}

How to change only p4, for example, and parameters p1,p3,p3 was still default?
Next time I want to change for example only p2, and parameters p1,p3,p4 was still default?
etc.

A: 

You can't. You can leave p3 and p4 out and they will use the default value when you want to only specify p2. But then you'd have to enter a value for p1 too.

frankhermes
I think idea Daniel Hai is super. Resolved my problem. :)
A: 

you could always do something like (but I don't think it's a great idea):

private function test(a1:Object=null, a2:Object=null, a3:Object = null, a4:Object = null):void {
    var p1:int      = (a1 !== null ? int(a1) : 3);
    var p2:Boolean  = (a2 !== null ? Boolean(a2) : true);
    var p3:uint     = (a3 !== null ? uint(a3) : 0xFFFF00);
    var p4:Number   = (a4 !== null ? Number(a4) : 55.5);
}

that way if you want something to be default, you can just pass in null:

with:

test(null,false,null,null);

but again, it's a bad idea. Maybe make the parameter an object -- it sounds like you're passing in a colortransform object -- which already has rgb + alpha + transparency? (just a wild guess)

Daniel Hai
It's very good idea. All is OK. I replaced in function class :void to :Array and in function wrote linereturn [p1,p2,p3,p4];Next call function trace(test(null,false,null,125));All is super.Thank you.