带有多个逗号分隔值的return语句 [英] return statement with multiple comma separated values

查看:2060
本文介绍了带有多个逗号分隔值的return语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Javascript语法:逗号是什么意思?

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

What does this pattern return? How does it work?

返回myfunc(),myobj.myvar = someobj.prop,myobj

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

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.

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

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

推荐答案

这是逗号运算符。它评估其左侧操作数,抛出结果,评估其右侧操作数,并将其作为结果值。它是从左到右的关联,所以 a,b,c 评估 a ,然后 b ,然后 c ,并将 c 的结果作为其值。

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, c evaluates a, then b, then c, and takes the result of c as its value.

在你的例子中,它完全像:

In your example, it's exactly like:

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

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

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

Since you've said it's a minifier: The very small possible 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;
}

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

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

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

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

...rather than this (65 characters):

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

...而不更改代码的功能,如果后面的内容是} 或其他一些合适的字符(或文件结尾)在最后触发自动分号插入。否则,它将需要第一个; ,但仍然可以保存一个字符。

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

这篇关于带有多个逗号分隔值的return语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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