开发者

Default parameters in ActionScript 3.0

开发者 https://www.devze.com 2022-12-28 02:38 出处:网络
I have a function, for example function test(p1:int=7,p2:Boolean=true,p3:uint=0xffff00,p4:Number=55.5)

I have a function, for example

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

//instructions

}

How to 开发者_C百科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.


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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消