JS Ternary operator confusion

别说谁变了你拦得住时间么 提交于 2019-12-23 16:59:46

问题


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


回答1:


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).




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/8916679/js-ternary-operator-confusion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!