多语句三元运算符

这是有效的 JavaScript 吗?我看到一个例子,有人在三元操作符条件中使用逗号,它在我的编辑器中被标记为错误,这个例子没有在 Chrome 中运行。不过,它确实可以在 Firefox 中运行。一旦我将所有的三元语句转换成 if/else 语句,这个应用程序就可以在 Chrome 上运行了。

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

Edit:

这是代码中的实际语句:

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
113416 次浏览

是的,它是有效的,并且在 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);

我不是说这是一个远程的好主意代码 人类是为了阅读。* 我希望 Jamietre 在他/她说这看起来像是缩小的结果时所作的评论是正确的。

逗号运算符是一个二进制运算符(接受两个操作数的运算符)。它计算左边的操作数(从而导致任何副作用,比如赋值) ,抛弃结果,然后计算右边的操作数(从而导致任何副作用) ,并将结果作为结果值。如果一行中有多个逗号运算符,则整个表达式按照从左到右的顺序进行计算,最终结果是最右边的操作数计算得到的值。

当然,你知道条件运算符(一个三元运算符 & mdash; 一个接受三个操作数)是用来在初始表达式的基础上从两个子表达式中选择一个进行求值的。

所以这一行非常... 有表现力... 里面总共有 * 不同的表达式。

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.

在我看来,正是这些副作用让它成为一个值得怀疑的选择。当然,如果左操作数 没有有副作用,就没有理由使用逗号操作符。


* 是的,他们的 包装成整体三元组:

  • a !== b
  • 第一个逗号表达式
  • a = 1
  • b = 2
  • 第二个逗号表达式
  • a = 2
  • b = 1

重新编辑你的实际声明,这一个也工作:

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

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

是的:

a=1;
b=2;


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


console.log(a);     // 1
console.log(b);     // 2

以及:

a=1;
b=2;


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


console.log(a);     // 2
console.log(b);     // 1

正如您可以分析的那样,如果您查看结果,更改相等运算符会对我们的测试作出正确的反应。

或者你可以这样做:

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

Read 给你 about comma operator.

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

Expanding on this topic with ES6 code example. If you're using one side of the TRUE : FALSE argument to iterate thru all cases in one IF, it makes sense to separate the code as if it's a switch | case statement.

嵌套意味着存在分支逻辑,虽然它在逻辑上是嵌套的,但是编写嵌套 IF 会使我们在本例中所做的工作复杂化。就像律师向陪审团解释问题一样。我的意思是,你想用最简单的形式来解释这一点。例如,我发现这个例子是表示执行 TRUE 的嵌套 if 的最合乎逻辑的方法。最后一个 false 是您的最后一个 else {} 舞门可以是01或2:

choreDoor === 0 ?
(openDoor1 = botDoorPath,
openDoor2 = beachDoorPath,
openDoor3 = spaceDoorPath)
: choreDoor === 1 ?
(openDoor2 = botDoorPath,
openDoor1 = beachDoorPath,
openDoor3 = spaceDoorPath)
: choreDoor === 2 ?
(openDoor3 = botDoorPath,
openDoor1 = beachDoorPath,
openDoor2 = spaceDoorPath)
: false;

如果不想使用 逗号运算符(,),那么可以使用嵌套的 条件(三元)运算符

var a = 6;
var b = 7;
var c = (a !== b)?  // true
((a = 1 || 1===1)? (b = 2) : null) // will first run a=1, then b=2
: ((a = 0 || 1===1)? (b = 0) : null);


console.log("a = " + a);
console.log("b = " + b);
console.log("c = " + c);