声明 int 类型的可选参数并测试其存在

发布于 2024-11-28 15:30:22 字数 457 浏览 0 评论 0原文

在 ActionScript 3 中,您可以像这样声明可选参数:

function (i:int = 0, s:String = "", o:Object = null):void { }

因此,您可以检查用户是否传递了参数 s 和 o,因为您可以测试空字符串或空对象 if(s && o)...

但是如何做你允许 int 真正是可选的吗?如果 i 的所有值都有效(包括 0、负整数和正整数)怎么办?如果您想强制执行整数(而不是使用数字?),那么

这里的最佳实践是什么? (...) 其余部分可能有效,但是您无法在运行时强制执行一定数量的参数,也无法完成有用的代码?

我正在尝试实现一个 margin(top:int, right:int, Bottom:int, left:int) 方法,该方法允许 right、bottom 和 left 是可选的。有什么想法吗?

In ActionScript 3 you can declare optional parameters like this:

function (i:int = 0, s:String = "", o:Object = null):void { }

So you can check if the user passed parameters s and o because you can test against the empty string or the null object if(s && o)...

But how do you allow an int to be truly optional? What if all values for i are valid, including 0, negative and positive integers? And what if you want to enforce integer (not use Number?)

what's the best practice here? the (...) rest may work but then you can't enforce a certain number of parameters at runtime, nor can you make for useful code completion?

I'm trying to implement a margin(top:int, right:int, bottom:int, left:int) method that allows right, bottom and left to be optional. Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡墨 2024-12-05 15:30:22

您可以使用 int.MAX_VALUEint.MIN_VALUE。请参阅此处的文档。

You can use either int.MAX_VALUE or int.MIN_VALUE. See documentation here.

彻夜缠绵 2024-12-05 15:30:22

您可以使用NaN来检查用户是否设置了该参数,但您需要使用Number而不是int。对于设置边距之类的事情,它可能不会有任何区别,因为它可能不会每秒被调用数千次。

function margin(top:Number, right:Number = NaN, bottom:Number = NaN, left:Number = NaN) {
    // Then here test with isNaN(right), isNaN(bottom), etc.
}

You can use NaN to check whether a user has set the parameter or not, but you need to use Number instead of int. For something like setting margins it probably won't make any difference, since it probably won't be called thousands of time per second.

function margin(top:Number, right:Number = NaN, bottom:Number = NaN, left:Number = NaN) {
    // Then here test with isNaN(right), isNaN(bottom), etc.
}
っ左 2024-12-05 15:30:22

如果所有可能的 int 值都是有效的(即您不能将 -1 指定为特殊的“未提供”值),那么您将无法使用 int数据类型。

我的建议是这样的(基于 Laurent 的答案):

将类型定义为 Number=NaN,并使用 isNaN() 测试它是否存在。

如果用户提供了一个值,则将该值转换为 int,否则为其指定默认值。

function margin(top:Number, right:Number = NaN, bottom:Number = NaN, left:Number = NaN) {
    // Then here test with isNaN(right), isNaN(bottom), etc.
    if (isNaN(right))
    {
        right = DEFAULT_MARGIN;    // some default value.

        // Any other logic here...
    }
    else
    {
        // Enforce integer values.
        right = int(right);    // or use Math.floor(right);
    }
}

您还可以使用三元运算符来减少行数:

function margin(top:Number, right:Number = NaN, bottom:Number = NaN, left:Number = NaN) {
    // Then here test with isNaN(right), isNaN(bottom), etc.

    // This is equivalent to the example above:
    right = isNaN(right) ? DEFAULT_MARGIN : int(right);
}

If all possible int values are valid (i.e. you can't designate -1 as a special 'not supplied' value) then you won't be able to use the int datatype.

My suggestion is this (based on Laurent's answer):

Define the type as Number=NaN, and test for it's existence with isNaN().

If the user has supplied a value then convert the value to an int, otherwise assign it your default value.

function margin(top:Number, right:Number = NaN, bottom:Number = NaN, left:Number = NaN) {
    // Then here test with isNaN(right), isNaN(bottom), etc.
    if (isNaN(right))
    {
        right = DEFAULT_MARGIN;    // some default value.

        // Any other logic here...
    }
    else
    {
        // Enforce integer values.
        right = int(right);    // or use Math.floor(right);
    }
}

You can also use the ternary operator to reduce the number of lines:

function margin(top:Number, right:Number = NaN, bottom:Number = NaN, left:Number = NaN) {
    // Then here test with isNaN(right), isNaN(bottom), etc.

    // This is equivalent to the example above:
    right = isNaN(right) ? DEFAULT_MARGIN : int(right);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文