为什么键入的可选参数不能具有默认值 Null? [英] Why can't typed optional arguments have a default of Null?

查看:32
本文介绍了为什么键入的可选参数不能具有默认值 Null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ActionScript 3 中,当您通过为其提供默认值来声明可选参数时,值 null 不能用于类型化参数.

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 更改为 Number 然后可以将其设置为 NaN,这是一个特殊的数字,表示不是数字",这可以表示您的空状态一个数字.

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);
}

对于所有其他对象,您可以将它们设置为 null,但原始"数字在 Actionscript 中的处理方式不同.

For all other objects, you can set them to null, but 'primitive' numbers are handled differently in Actionscript.

这篇关于为什么键入的可选参数不能具有默认值 Null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆