简体   繁体   English

这是JavaScript逗号运算符的一种情况

[英]Is this a case of javascript comma operator

Please see this code from d3.js 请从d3.js查看代码

if (y1 < y0) t = y0, y0 = y1, y1 = t;

has this anything to do with comma operator , in the sense of returning/assigning the last value, or equivalent to 在返回/分配最后一个值的意义上,它与逗号运算符有任何关系,或者等于

if (y1 < y0) {
  t = y0;
  y0 = y1;
  y1 = t;
}

I was more worried about, anything tricky happening here by relying on the order of operation. 我更担心,依靠操作顺序在这里发生任何棘手的事情。

Yes, the first snippet uses the comma operator and both snippets are equivalent, as the comma operator evaluates from left to right. 是的,第一个代码段使用逗号运算符,并且两个代码段都是等效的,因为逗号运算符从左到右求值。

The comma operator is used in your snippet to put multiple assignments into a single statement , so they are all governed by the preceding if and a few characters ( { and } ) can be saved. 您的代码段中使用逗号运算符将多个分配放入单个语句中 ,因此它们全部由前面的if和几个字符( {} )保存。

Not sure if this is worth doing manually, but minifiers typically do this kind of optimization (and it adds up) 不知道这是否值得手动执行,但是缩小器通常会进行这种优化(累加起来)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM