Actionscript 3 如何为…params函数参数设置默认值?
我有一些实现此接口的类:Actionscript 3 如何为…params函数参数设置默认值?,actionscript-3,function,parameters,default-value,Actionscript 3,Function,Parameters,Default Value,我有一些实现此接口的类: function execute(entity:Entity, ...params):void; 没关系,不过我想要这个: function execute(entity:Entity, ...params = null):void; 因为,不是每个类都需要参数 它抛出一个编译错误 似乎我无法在AS3中为…参数设置默认值。有办法吗 谢谢。我不知道有什么方法可以在声明时将参数的默认值设置为空数组以外的值,但解决方法如下: function exec(entit
function execute(entity:Entity, ...params):void;
没关系,不过我想要这个:
function execute(entity:Entity, ...params = null):void;
因为,不是每个类都需要参数
它抛出一个编译错误
似乎我无法在AS3中为…参数设置默认值。有办法吗
谢谢。我不知道有什么方法可以在声明时将
参数的默认值设置为空数组以外的值,但解决方法如下:
function exec(entity:Entity, ... extraParams)
{
// EDIT: strange that you are getting null,
// double check your variable names and if needed you can add:
if(extraParams == null)
{
extraParams = new Array();
}
if(extraParams.length == 0) // If none are specified
{
// Add default params
extraParams[0] = "dude";
extraParams[1] = "man";
}
// the rest of the function
}
我不知道有什么方法可以将params
的默认值设置为声明点处的空数组以外的值,但解决方法如下:
function exec(entity:Entity, ... extraParams)
{
// EDIT: strange that you are getting null,
// double check your variable names and if needed you can add:
if(extraParams == null)
{
extraParams = new Array();
}
if(extraParams.length == 0) // If none are specified
{
// Add default params
extraParams[0] = "dude";
extraParams[1] = "man";
}
// the rest of the function
}
不能像那样在方法签名中设置默认值,但可以在函数体中执行类似于params=params | |“defaultVal”的操作代码>我不喜欢这个解决方案tbh:pDo您需要指定默认值null吗?我相信用…调用函数是可以的。。。params没有实际传递任何额外的参数。无法在方法签名中设置默认值,但可以在函数体中执行类似于params=params | |“defaultVal”的操作代码>我不喜欢这个解决方案tbh:pDo您需要指定默认值null吗?我相信用…调用函数是可以的。。。没有实际传递任何额外参数的params。无法访问null或未定义参数的成员长度
。如果我的编辑有帮助,我在上面的代码中添加了null检查。还要确保变量名与实际代码匹配。无法访问空参数或未定义参数的成员长度
。如果我的编辑有帮助,我在上面的代码中添加了空检查。还要确保变量名在实际代码中匹配。