Javascript - 具有多个语句的三元运算符 [英] Javascript - Ternary Operator with Multiple Statements

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

问题描述

这是有效的JavaScript吗?我看到有人在三元运算符条件下使用逗号的示例,并且在我的编辑器中将其标记为错误,并且示例未在Chrome中运行。但是,它确实在Firefox中运行。将所有三元语句转换为if / else语句后,应用程序在Chrome上运行。

Is this valid JavaScript? I saw an example where someone used commas in the ternary operator conditions, and it was marked as an error in my editor, and the example didn't run in Chrome. However, it did run in Firefox. Once I converted all the ternary statements to if/else statements, the app ran on Chrome.

a!==b ? (a=1, b=2) : (a=2, b=1)

编辑:

这是代码中的实际陈述:

This is the actual statement in the code:

a!==0?b<0?(h=b/a,e=h-1,f=-2*b+2*a*e,i=-2*b+2*a*h,d=2*h*a-2*b-2*a):(h=b/a,e=h+1,f=2*b-2*a*e,i=2*b-2*a*h,d=-2*h*a+2*b):d=h=e=f=i=0


推荐答案

是的,它有效,并且在Chrome中运行正常:

Yes, it's valid, and it runs fine in Chrome:

var a, b, c;

a = 6;
b = 7;
c = a !== b ? (a = 1, b = 2) : (a = 2, b = 1);
console.log("a = " + a);
console.log("b = " + b);
console.log("c = " + c);

我不是说这是一个代码 human 中的远程好主意是为了阅读。 :-)我希望jamietre在评论中是正确的,当他/她说它看起来像是缩小的结果。

I'm not saying it's a remotely good idea in code humans are meant to read. :-) I expect jamietre is correct in the comments when he/she says it looks like the result of minification.

逗号运算符是一个二元运算符(接受两个操作数的运算符)。它评估它的左侧操作数(因此导致它具有的任何副作用,例如赋值),抛出该结果,然后评估其右侧操作数(从而导致其副作用,如果有的话)并将该结果作为其结果值。如果连续有多个逗号运算符,则按从左到右的顺序计算整个表达式,最终结果是从最右边的操作数求值得到的值。

The comma operator is a binary operator (an operator accepting two operands). It evaluates its left-hand operand (thus causing any side-effects it has, such as assignment), throws that result away, then evalutes its right-hand operand (thus causing its side-effects if any) and takes that result as its result value. If you have multiple comma operators in a row, the overall expression is evaluated in order, left-to-right, with the final result being the value resulting from the right-most operand evaluation.

当然,你知道条件运算符(一个三元运算符—一个接受三个操作数)用于根据初始表达式选择要评估的两个子表达式之一。

And of course, you know the conditional operator (a ternary operator — one accepting three operands) is used to pick one of two sub-expressions to evaluate, on the basis of an initial expression.

因此该行非常......富有表现力......其中包含七个 *不同表达式的内容。

So that line is very...expressive...what with a total of seven* different expressions inside it.

因此,在该示例中,如果最初 a!== b ,则整个表达式的结果为2,或者 1 如果 a === b 最初,带有设置 a 的副作用, b

So in that example, the result of the overall expression is 2 if a !== b initially, or 1 if a === b initially, with the side-effects of setting a and b.

在我看来,这是副作用,这是一个值得怀疑的选择。当然,如果左侧操作数没有有副作用,则没有理由使用逗号运算符。

It's the side effects that make it, in my view, a questionable choice. And of course, there's no reason to use the comma operator if the left-hand operand doesn't have side effects.

*是的,七个包含在整个三元组中:

* Yes, seven of 'em packed into that overall ternary:


  • a!== b

  • 第一个逗号表达式

  • a = 1

  • b = 2

  • 第二个逗号表达

  • a = 2

  • b = 1

  • a !== b
  • the first comma expression
  • a = 1
  • b = 2
  • the second comma expression
  • a = 2
  • b = 1

使用实际声明重新编辑也适用:

Re your edit with the actual statement, that one works too:

function test(a) {
    var b = 7,
        d = 1,
        e = 2,
        f = 3,
        g = 4,
        h = 5,
        i = 6;
    
    a!==0?b<0?(h=b/a,e=h-1,f=-2*b+2*a*e,i=-2*b+2*a*h,d=2*h*a-2*b-2*a):(h=b/a,e=h+1,f=2*b-2*a*e,i=2*b-2*a*h,d=-2*h*a+2*b):d=h=e=f=i=0;
    
    console.log("a = " + a);
    console.log("b = " + b);
    console.log("d = " + d);
    console.log("e = " + e);
    console.log("f = " + f);
    console.log("g = " + g);
    console.log("h = " + h);
    console.log("i = " + i);
}

test(0);
test(1);

.as-console-wrapper {
  max-height: 100% !important;
}

但哇,我希望这会缩小,因为如果一个人写了这个,他们必须真的对任何应该维护的人有所作为它稍后......; - )

But wow, I hope this is minified, because if a person wrote that, they must really have a thing against anyone who's supposed to maintain it later... ;-)

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

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