JavaScript 中参数的位掩码类型

发布于 2024-12-13 02:55:58 字数 189 浏览 4 评论 0原文

我发现 PHP 中有些函数可以在单个参数中接受多个选项,例如:

error_reporting(E_ERROR & E_WARNING);

我想在我的 javascript 函数中实现这种功能。您知道采用此类参数的函数示例吗?

我完全困惑该函数如何知道传递的选项是什么......

I see that in PHP there are functions that can accept multiple options into a single argument, for example:

error_reporting(E_ERROR & E_WARNING);

I want to implement this kind of functionality into my javascript function. Do you know examples of functions that take this kind of arguments?

I'm totally confused about how does the function know what are the options that are passed...

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

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

发布评论

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

评论(2

糖粟与秋泊 2024-12-20 02:55:58

(假设它是: error_reporting(E_ERROR | E_WARNING); )

好吧,它是一个位掩码。因此,E_ERROR 可能是这样的数字(二进制​​):

0010000

,E_WARNING 可能是:

0000010

,当它们与 | 组合时,您会得到

0010010

(单个整数,设置了多个位)

然后该函数可以检查设置了哪些位使用 &按位运算符。您当然可以在 javascript 中完成此操作,尽管它可能不太常见,并且随着数字变大,效率和可靠性可能会降低,因为 javascript 并不真正具有整数,它们实际上是浮点数。

在 javascript 中,您可能更喜欢使用这样的东西:(

error_reporting({warning: true, error: true});

编辑以更合理的方式显示它,用 | not & 将它们组合起来)

(Assume it is instead: error_reporting(E_ERROR | E_WARNING); )

Well, it is a bitmask. So E_ERROR might be a number like this (in binary):

0010000

and E_WARNING might be:

0000010

and when they are combined with |, you get

0010010

(a single integer, with multiple bits set)

The function can then check which bits are set using the & bitwise operator. You can certainly do it in javascript, although it might be less common, and possibly less efficient and reliable as the numbers get bigger since javascript doesn't really have integers, they are really floating point numbers.

In javascript, you might prefer to use something like this:

error_reporting({warning: true, error: true});

(edited to show it in the more sensible way, with | not & to combine them)

薄荷→糖丶微凉 2024-12-20 02:55:58

是的,这是可能的,但它适用于 |

var flag1 = 1,
    flag2 = 2; // powers of 2

function get(flags) {
    if((flags & flag1) === flag1) { // remove all other bits and check
        alert('flag1');             // whether you get the flag itself
    }

    if((flags & flag2) === flag2) {
        alert('flag2');
    }
}

get(flag1 | flag2); // alerts both

您当然可以创建一个辅助函数来执行检查,这样您就不需要这些括号:

function contains(flags, flag) {
    return (flags & flag) === flag;
}

http: //jsfiddle.net/pimvdb/NpFYj/1/

Yes, it's possible, but it works with |.

var flag1 = 1,
    flag2 = 2; // powers of 2

function get(flags) {
    if((flags & flag1) === flag1) { // remove all other bits and check
        alert('flag1');             // whether you get the flag itself
    }

    if((flags & flag2) === flag2) {
        alert('flag2');
    }
}

get(flag1 | flag2); // alerts both

You could of course create a helper function which does the chekcing so that you don't need those parentheses:

function contains(flags, flag) {
    return (flags & flag) === flag;
}

http://jsfiddle.net/pimvdb/NpFYj/1/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文