JS 三元运算符混淆

发布于 2024-12-27 15:14:35 字数 1005 浏览 1 评论 0原文

我现在正在学习三元运算符。我已经掌握了基础知识,但后来我看到了这个片段,它对我来说没有任何意义。谁能解释一下它是如何组合在一起的?

b.m & 4 || (c |= 2, 63 <= a && 77 >= a ? a = 65 : 48 <= a && 57 >= a ? a = 48 : c & 1 ? 97 <= a && 122 >= a ? a = 65 : 197 == a || 229 == a ? c &= 5 : 192 <= a && 687 >= a ? a = 192 : 1536 <= a ? a = 1536 : 912 <= a ? a = 912 : 160 <= a ? a = 160 : 127 <= a ? c &= 5 : 33 <= a ? a = 59 : c &= 5 : 48 > a ? c &= 5 : 65 > a ? a = 59 : 96 > a ? c &= 5 : 112 > a ? a = 96 : 187 > a ? c &= 5 : a = 59);

bm& 4 || 据我理解是位运算,然后 (c |= 2, 另一个位运算,但是逗号是什么意思?!

然后是 [第 63 回] 77 >= 一个? a = 65 : 48

转换为

if(a >= 63 && a <= 77){ 一个= 65 } 别的 { a = 48; }

然后是 <= a && 57 >= 一个? a = 48:c & 1 ? 97 <= a 这对我来说根本没有任何意义。因为 48 是针对前一个语句的。任何人都可以

I'm learning about ternary operators now. I got the basics down, but then I saw this snippet and it doesn't make any sense to me. Can anyone please explain how is it put together?!

b.m & 4 || (c |= 2, 63 <= a && 77 >= a ? a = 65 : 48 <= a && 57 >= a ? a = 48 : c & 1 ? 97 <= a && 122 >= a ? a = 65 : 197 == a || 229 == a ? c &= 5 : 192 <= a && 687 >= a ? a = 192 : 1536 <= a ? a = 1536 : 912 <= a ? a = 912 : 160 <= a ? a = 160 : 127 <= a ? c &= 5 : 33 <= a ? a = 59 : c &= 5 : 48 > a ? c &= 5 : 65 > a ? a = 59 : 96 > a ? c &= 5 : 112 > a ? a = 96 : 187 > a ? c &= 5 : a = 59);

b.m & 4 || are bit operations as far as I understood, then (c |= 2, another bit operation, but what does comma mean?!

Then there's
63 <= a && 77 >= a ? a = 65 : 48

which translates to

if(a >= 63 && a <= 77){
a = 65
} else {
a = 48;
}

and then after that comes <= a && 57 >= a ? a = 48 : c & 1 ? 97 <= a which doesn't make any sense to me at all. because 48 was for the previous statement. Can anyone

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

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

发布评论

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

评论(3

如梦初醒的夏天 2025-01-03 15:14:35

逗号是 JavaScript 中的一个单独的运算符

逗号运算符计算其两个操作数(从左到右)
并返回第二个操作数的值。

你只掌握了表达的一部分:

然后有 63 <= a && 77 >= 一个? a = 65 : 48

实际上它有点长(有一些格式):

63 <= a && 77 >= a
    ? a = 65
    : 48 <= a && 57 >= a
        ? a = 48
        : c & 1
            ? 97 <= a && 122 >= a
                ? a = 65
                : 197 == a || 229 == a
                    ? c &= 5
                    : 192 <= a && 687 >= a
                        ? a = 192
                        : 1536 <= a
                            ? a = 1536
                            : 912 <= a
                                ? a = 912
                                : 160 <= a
                                    ? a = 160
                                    : 127 <= a
                                        ? c &= 5
                                        : 33 <= a
                                            ? a = 59
                                            : c &= 5
            : 48 > a
                ? c &= 5
                : 65 > a
                    ? a = 59
                    : 96 > a
                        ? c &= 5
                        : 112 > a
                            ? a = 96
                            : 187 > a
                                ? c &= 5
                                : a = 59

尝试用 if-else 方式重写它会产生以下结果:

if (63 <= a && 77 >= a){
    a = 65
} else if (48 <= a && 57 >= a){
    a = 48
} else if (c & 1){
    if (97 <= a && 122 >= a){
        a = 65
    } else if (197 == a || 229 == a){
        c &= 5
    } else if (192 <= a && 687 >= a){
        a = 192
    } else if (1536 <= a){
        a = 1536
    } else if (912 <= a){
        a = 912
    } else if (160 <= a){
        a = 160
    } else if (127 <= a){
        c &= 5
    } else if (33 <= a){
        a = 59
    } else {
        c &= 5
    }               
} else {
    if (48 > a){
        c &= 5
    } else if (65 > a){
        a = 59
    } else if (96 > a){
        c &= 5
    } else if (112 > a){
        a = 96
    } else if (187 > a){
        c &= 5
    } else {
        a = 59
    }
}

注意 if-else 方法缺乏返回当前值,而三元运算符确实返回最后执行的运算符的值(这可能会影响括号中布尔表达式的整体值)。

Comma is a separate operator in javascript:

The comma operator evaluates both of its operands (from left to right)
and returns the value of the second operand.

You have grasped just a part of expression:

Then there's 63 <= a && 77 >= a ? a = 65 : 48

Actually it is a little bit longer (with some formatting):

63 <= a && 77 >= a
    ? a = 65
    : 48 <= a && 57 >= a
        ? a = 48
        : c & 1
            ? 97 <= a && 122 >= a
                ? a = 65
                : 197 == a || 229 == a
                    ? c &= 5
                    : 192 <= a && 687 >= a
                        ? a = 192
                        : 1536 <= a
                            ? a = 1536
                            : 912 <= a
                                ? a = 912
                                : 160 <= a
                                    ? a = 160
                                    : 127 <= a
                                        ? c &= 5
                                        : 33 <= a
                                            ? a = 59
                                            : c &= 5
            : 48 > a
                ? c &= 5
                : 65 > a
                    ? a = 59
                    : 96 > a
                        ? c &= 5
                        : 112 > a
                            ? a = 96
                            : 187 > a
                                ? c &= 5
                                : a = 59

Trying to rewrite it in if-else fasion will yield the following result:

if (63 <= a && 77 >= a){
    a = 65
} else if (48 <= a && 57 >= a){
    a = 48
} else if (c & 1){
    if (97 <= a && 122 >= a){
        a = 65
    } else if (197 == a || 229 == a){
        c &= 5
    } else if (192 <= a && 687 >= a){
        a = 192
    } else if (1536 <= a){
        a = 1536
    } else if (912 <= a){
        a = 912
    } else if (160 <= a){
        a = 160
    } else if (127 <= a){
        c &= 5
    } else if (33 <= a){
        a = 59
    } else {
        c &= 5
    }               
} else {
    if (48 > a){
        c &= 5
    } else if (65 > a){
        a = 59
    } else if (96 > a){
        c &= 5
    } else if (112 > a){
        a = 96
    } else if (187 > a){
        c &= 5
    } else {
        a = 59
    }
}

Please, pay attention that if-else approach lacks returning value currently, whereas ternary operator does return the value of the last operator executed (this may affect the overall value of boolean expression in parentheses).

不再让梦枯萎 2025-01-03 15:14:35

我更喜欢格式化嵌套三元语句像这样,所以它们有一个简单、可读的结构:

//   (is this true) ? then do this  
//   (is this true) ? then do this  
// (all else fails) : then do this

遵循该结构,它看起来像这样:

  63 <= a && 77 >= a ? a = 65
: 48 <= a && 57 >= a ? a = 48
:              c & 1 ? /* then go into this indented block below */
                         97 <= a && 122 >= a ? a = 65
:                       197 == a || 229 == a ? c &= 5
:                       192 <= a && 687 >= a ? a = 192
:                                  1536 <= a ? a = 1536
:                                   912 <= a ? a = 912
:                                   160 <= a ? a = 160
:                                   127 <= a ? c &= 5
:                                    33 <= a ? a = 59
                                  /* else */ : c &= 5
:            48 > a ? c &= 5
:            65 > a ? a = 59
:            96 > a ? c &= 5
:           112 > a ? a = 96
:           187 > a ? c &= 5
   /* final else */ : a = 59

I prefer to format nested ternary statements like this, so they have a simple, readable structure:

//   (is this true) ? then do this  
//   (is this true) ? then do this  
// (all else fails) : then do this

Following that structure, it would look something like this:

  63 <= a && 77 >= a ? a = 65
: 48 <= a && 57 >= a ? a = 48
:              c & 1 ? /* then go into this indented block below */
                         97 <= a && 122 >= a ? a = 65
:                       197 == a || 229 == a ? c &= 5
:                       192 <= a && 687 >= a ? a = 192
:                                  1536 <= a ? a = 1536
:                                   912 <= a ? a = 912
:                                   160 <= a ? a = 160
:                                   127 <= a ? c &= 5
:                                    33 <= a ? a = 59
                                  /* else */ : c &= 5
:            48 > a ? c &= 5
:            65 > a ? a = 59
:            96 > a ? c &= 5
:           112 > a ? a = 96
:           187 > a ? c &= 5
   /* final else */ : a = 59
春花秋月 2025-01-03 15:14:35

逗号不是三元运算符,它允许两个表达式替换一个表达式。

但更重要的是,这是一团糟。将其分解、格式化并评论。除非你正在打高尔夫球,否则将所有这些合并成一堆简直就是虐待。

The comma isn't a ternary operator, it allows two expressions to replace one.

But more importantly, that's a mess. Break it down, format it, and comment it. Unless you're golfing, combining all of that into one pile is just abusive.

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