javascript 带有多个逗号分隔值的 return 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10284536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:19:30  来源:igfitidea点击:

return statement with multiple comma separated values

javascript

提问by Duncan Gravill

Possible Duplicate:
Javascript syntax: what comma means?

可能重复:
Javascript 语法:逗号是什么意思?

What does this pattern return? How does it work?

这个模式返回什么?它是如何工作的?

return myfunc(), myobj.myvar = someobj.prop, myobj

return myfunc(), myobj.myvar = someobj.prop, myobj

I have not come across this pattern before but have been looking at the Bing Maps Ajax Control and have noticed this pattern several times.

我以前没有遇到过这种模式,但一直在查看 Bing Maps Ajax Control 并多次注意到这种模式。

As I understand it multiple values are not returned. So what does this pattern do? What is returned? What is this pattern's benefit?

据我了解,不会返回多个值。那么这个模式有什么作用呢?返回什么?这种模式有什么好处?

回答by T.J. Crowder

It's the comma operator. It evaluates its left-hand operand, throws the result away, evalutes its right-hand operand, and takes that as its result value. It's left-to-right associative, so a, b, cevaluates a, then b, then c, and takes the result of cas its value.

它是逗号运算符。它评估其左侧操作数,将结果丢弃,评估其右侧操作数,并将其作为结果值。它是从左到右关联的,因此a, b, c计算a、 then b、 thenc并将 的结果c作为其值。

In your example, it's exactly like:

在你的例子中,它就像:

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

有些人真的更喜欢在一条线上做事,即使没有客观的理由。您给出的示例没有任何好处,实际上它令人困惑,因为它使前两位看起来与最终将返回的值相关,而实际上与此无关。(我在你告诉我们它是缩小代码之前写的;显然,人类不清楚只是源代码的问题,而不是缩小代码。)

Since you've said it's a minifier: The very smallpossible benefit the minifier might have gotten there is if this is part of a conditional block: It can save one or two characters. If we assume the long form looked like this:

既然你说它是一个缩小器:如果这是条件块的一部分,缩小器可能获得的非常小的好处是:它可以节省一两个字符。如果我们假设长格式如下所示:

if (someCondition) {
    myfunc();
    myobj.myvar = someobj.prop;
    return myobj;
}

...using the comma operator, the minifier can do this (63 characters):

...使用逗号运算符,缩小器可以执行此操作(63 个字符):

if(someCondition)return myfunc(),myobj.myvar=someobj.prop,myobj

...rather than this (65 characters):

...而不是这个(65 个字符):

if(someCondition){myfunc();myobj.myvar=someobj.prop;return myobj}

...without changing the functionality of the code, ifwhat follows is a }or some other appropriate character (or end-of-file) to trigger automatic semicolon insertionat the end. Otherwise, it would need the ;on the first one, but that still saves one character.

...在不改变代码功能的情况下,如果后面是一个}或其他适当的字符(或文件结尾)以在末尾触发自动分号插入。否则,它将需要;第一个,但这仍会保存一个字符。

回答by Duncan Gravill

The comma operatorevaluates (from left to right) the expressions and then it returns the last result, which in this case will be the evaluation of the myobjidentifier.

逗号操作符求值(从左至右)的表达式,然后将其返回最后结果,在这种情况下将是评价myobj标识符。

You can do this eliminate some curly braces if that's important to you...

如果这对您很重要,您可以这样做以消除一些花括号......

if (true)
    ;// do something
else
    return myfunc(), myobj.myvar = someobj.prop, myobj

...as opposed to...

……相对于……

if (true)
    ;// do something
else {
    myfunc();
    myobj.myvar = someobj.prop;
    return  myobj;
}

回答by Tobias Krogh

in your example myobj should be returned where as every thing before gets executed

在你的例子中 myobj 应该被返回,因为在执行之前的每件事

myfunc();
myobj.myvar = someobj.prop;
return myobj;