Default parameters in ActionScript 3.0

I have a function like

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

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

+2


a source to share


2 answers


you can always do something (but I don't think that'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);
}

      

thus, if you want something to be the default, you can simply pass null:



from:

test(null,false,null,null);

      

but again, this is a bad idea. Maybe make the parameter an object - it looks like you are passing in a colortransform object that already has rgb + alpha + transparent? (just a wild guess)

+1


a source


You can not. You can leave p3 and p4 and they will use the default if you only want to specify p2. But then you will also need to enter a value for p1.



0


a source







All Articles