JS三元运算符混乱 [英] JS Ternary operator confusion

查看:168
本文介绍了JS三元运算符混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在了解三元运算符.我掌握了基础知识,但是后来我看到了这个摘要,对我来说没有任何意义.谁能解释一下如何组装?!

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 || 是位运算,然后(c | = 2 )是另一位运算,但是逗号是什么意思!!

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

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

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

翻译为

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

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

,然后出现< = a&& 57> =一个? a = 48:c& 1个97< = 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

推荐答案

逗号是一个单独的

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:

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

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

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

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
    }
}

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

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

这篇关于JS三元运算符混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆