为什么不能输入可选参数都有空的默认? [英] Why can't typed optional arguments have a default of Null?
问题描述
在ActionScript 3中,当你给它一个默认值声明一个可选的参数,值空不能在类型参数使用。
In ActionScript 3, when you declare an optional argument by giving it a default value, the value null cannot be used on typed arguments.
function Action(Param:int=null){
// 1184: Incompatible default value of type Null where int is expected.
}
function Action(Param:int=0){
// No compiler errors
}
这个任何变通办法,或通用值,可以适用于所有数据类型?
Any workarounds for this, or general purpose values that can apply to all data types?
推荐答案
您可以将您的INT改变号码,然后可以将其设置为NaN这是一个特殊的数字,这意味着不是一个数字,这可以重新present的空状态的号码。
You can change your int to Number and then can set it to NaN which is a special number that means 'not a number' and this can represent your null state for a Number.
要检查,如果事情是NaN,则必须使用isNaN()函数,而不是 VAL == NaN的
,否则你不会得到你所期望的。
To check if something is NaN, you must use the isNaN() function and not val == NaN
, or you will not get what you expect.
function Action(param:Number = NaN) : void {
trace(param);
}
对于所有其他对象,你可以将其设置为空,但原始号在Actionscript中处理方式不同。
For all other objects, you can set them to null, but 'primitive' numbers are handled differently in Actionscript.
这篇关于为什么不能输入可选参数都有空的默认?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!