【问题标题】:Is there any way to assign default value to an array passed as parameter in function, in ActionScript?有没有办法在 ActionScript 中为函数中作为参数传递的数组分配默认值?
【发布时间】:2017-10-02 11:22:16
【问题描述】:

受保护的函数 testMethod(event:FlexEvent, args : Array = [] ):void { ... }

这不起作用,抛出错误:1047:参数初始化器未知或不是编译时常量。

那么有没有办法在 ActionScript 中为函数中作为参数传递的数组分配默认值?

【问题讨论】:

标签: arrays actionscript-3 flash


【解决方案1】:

此用例没有内置功能,但您可以像这样轻松解决它:

protected function testMethod(event:FlexEvent, args:Array):void{
    if(args == null || args.length == 0){
       args = ["these", "are", "my", "default", "values", 3, 5, false];

    }
    ....
}

或者,您可以查看... (rest) Parameter(页面底部)

【讨论】:

    【解决方案2】:

    将在别处定义的常量分配给参数作为默认值将解决该问题。 ;)

    【讨论】:

    • 我不确定...如果你将一些东西推送到这个数组怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多