AS3: Can we skip the optional parameter and assign a value to the parameter after the missing one?

Is it possible to skip an optional parameter and assign a value to the parameter after the missing one?

For example, I have a function:

public function Dialog(message:String,title:String="Note",dialogsize:int=99):void
{
}

I can easily call a function with a message and a header:

 Dialog("HELLO","Intro");

Is there a way to skip the title and just go through the dialog? I tried but can't make it work:

Dialog("HELLO",,dialogsize);

Can I skip some optional parameters without using the (rest) parameter?

+3
source share
3 answers

null, "defaulted", as3 - , :

Dialog("HELLO",null,dialogsize);

Edit

- , , ... ( , @www0z0k ) . , .

- :

public function Dialog(message:String,title:String=null,dialogsize:int=99):void
{
    if(title===null) title = "Note";

}
+6

, - :

public function Dialog(message:String, optionalArgs: Object):void{
    var title: String = optionalArgs['title'] ? optionalArgs['title'] : 'default value';
    var dialogsize: int =  optionalArgs['dialogsize'] ? optionalArgs['dialogsize'] : 99;
    var smthElse: String =  optionalArgs['smthElse'] ? optionalArgs['smthElse'] : 'another default val';
}

:
Dialog('msg', {dialogsize: 250, smthElse: 'another value'});

+1

value , :

// DialogVO.as
package
{
    public class DialogVO
    {
        public var message : String;
        public var title : String;
        public var size : int;
    }
}

// Test.as
public function createDialog(vo : DialogVO) : void
{
    if(vo.title)
        // write code for title here

    if(vo.message)
        // write code for meassage here

    if(vo.size)
        // write code for size here
}

// test your method
var dialogData : DialogVO = new DialogVO();
    dialogData.message = "This is the message";
    dialogData.size = 92;

createDialog(dialogData);
+1

Source: https://habr.com/ru/post/1780151/


All Articles