如何在 Flex3 中指示未设置的 Number 类型默认参数?
对于String和Object类型,我可以将默认参数设置为null,以表明它不是由调用者设置的。 flex3 中是否有一种机制可以对 Number 类型执行相同的操作?
例如: 公共函数 myMethod( stringVar:String=null, ObjectVar:Object=null, numberVar:Number ) { ... 我
可以执行以下操作,但感觉很丑
public function myMethod( numberVarObj:Object=null )
{
var numberVarSet:Boolean=true;
if( numberVarObj == null ) {
numberVarSet = false;
}
and then everywhere I want to use numberVar I can check for numberVarSet and cast as a Number.
for String and Object type, I can set the default parameter to null to indicate that it was not set by the caller. Is there a mechanism in flex3 to do the same for the Number type?
So for instance:
public function myMethod( stringVar:String=null, ObjectVar:Object=null, numberVar:Number )
{
...
}
I could do the following, but it just feels ugly
public function myMethod( numberVarObj:Object=null )
{
var numberVarSet:Boolean=true;
if( numberVarObj == null ) {
numberVarSet = false;
}
and then everywhere I want to use numberVar I can check for numberVarSet and cast as a Number.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你总是可以尝试:
然后在你想要的时候将其设置为一个数字。 。 。不过,如果有一个强类型的解决方案就好了。
另一个选项,如 Adobe 文档中指定(向下滚动到默认值),会将值 NaN 视为 null。但是,如果您的数据有任何可能包含 NaN 值,那么这是一个可怕的想法。
I suppose you could always try:
And then set it to a number when you want . . . It would be nice to have a solution that is strongly typed though.
Another option, as specified in Adobe's Docs (scroll down to default values), would be to treat the value NaN as null. However, if your data has ANY chance of containing a NaN value, this is a horrible idea.
我推荐您使用的“丑陋”解决方案,但如果您确实想要另一个选择,您可以使用 NaN,然后使用 isNaN(num) 来检查值。
I'd recommend the "ugly" solution you have, but if you really want another option you can use NaN and then use isNaN(num) to check the value.