这是逗号运算符 http://es5.github.com/#x11.14。它评估其左侧操作数,丢弃结果,评估其右侧操作数,并将其作为结果值。它是从左到右关联的,所以a, b, c
评估a
, then b
, then c
,并取结果c
作为它的价值。
在您的示例中,它完全类似于:
myfunc();
myobj.myvar = someobj.prop;
return myobj;
Some people really prefer to do things on one line, even when there's no objective reason to. There no benefit in the example you gave, and in fact it's confusing because it makes it look like the first two bits relate to the value that will ultimately be returned, which they don't. (I wrote that before you told us it was minified code; obviously, being unclear to humans is only an issue in source code, not minified code.)
既然你已经说过它是一个缩小器:很小如果这是条件块的一部分,缩小器可能获得的好处是:它可以保存一两个字符。如果我们假设长形式如下所示:
if (someCondition) {
myfunc();
myobj.myvar = someobj.prop;
return myobj;
}
...使用逗号运算符,缩小器可以执行此操作(63 个字符):
if(someCondition)return myfunc(),myobj.myvar=someobj.prop,myobj
...而不是这个(65 个字符):
if(someCondition){myfunc();myobj.myvar=someobj.prop;return myobj}
...在不改变代码功能的情况下,if接下来是}
或其他一些适当的字符(或文件结尾)来触发自动插入分号 http://es5.github.com/#x7.9在最后。否则,就需要;
在第一个上,但这仍然保存了一个字符。