Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #22464

Re: How to understand return a,b statement ?

From Joao Rodrigues <groups_jr-1@yahoo.com.br>
Newsgroups comp.lang.javascript
Subject Re: How to understand return a,b statement ?
Date 2013-12-28 23:16 -0200
Organization Aioe.org NNTP Server
Message-ID <l9nt4h$6m3$1@speranza.aioe.org> (permalink)
References <ec287c43-ac92-489e-8140-55272ace07bb@googlegroups.com> <1368da15-53d4-457c-9ee1-3e9f90b677d2@googlegroups.com> <l9n3ka$d47$1@speranza.aioe.org> <l9ndpk$d2v$1@dont-email.me>

Show all headers | View raw


On 28/12/2013 18:54, Denis McMahon wrote:
> On Sat, 28 Dec 2013 16:00:46 -0200, Joao Rodrigues wrote:
>
>> According to Douglas Crockford's book (JavaScript: The Good Parts), the
>> comma operator should be used only inside for statements
>
> If I might suggest another use of the comma:
>
> var a=2, b=6, c="fred", d={'time':0,amount:'0','items':[]};

Well, these commas are mere separators in the variable statement (ES 5.1 
§ 12.2). The comma is also used to separate elements of Arrays and 
Objects. But the comma operator (ES 5.1 § 11.14) is something else.

E.g of comma separator:

var colorIndex = 0,
     colors = ["FF0000", "008000", "FF0086", "A2FF00",
               "0000FF", "800080"];

E.g of the comma operator:

function selectNextColor() {
   return colors[colorIndex++] || colors[colorIndex = 0, colorIndex++];
}


JSLint expects to find the comma operator only inside for loops:

for (var i = 0, len = arr.length; i < len; i++) {...}

But we can use the comma operator in other parts of our code:

var el, len = elems.length - 1;
while (el = elems[len], len--) {
   if (el.type === 'checkbox') {
     //...
   }
}

var s = '{"foo": "bar"}';
var o = eval("1," + s);

Or:

var s = "foo";
1, {
      "foo": ["baz", "bar"]
    }[s].sort();

-- 
Joao Rodrigues

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to understand return a,b statement ? Guillaume Biton <guillaume.biton@gmail.com> - 2013-12-28 08:57 -0800
  Re: How to understand return a,b statement ? Guillaume Biton <guillaume.biton@gmail.com> - 2013-12-28 09:03 -0800
    Re: How to understand return a,b statement ? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2013-12-28 16:00 -0200
      Re: How to understand return a,b statement ? Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-28 20:54 +0000
        Re: How to understand return a,b statement ? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2013-12-28 23:16 -0200
        Re: How to understand return a,b statement ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-12-29 17:25 +0100
        Re: How to understand return a,b statement ? Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-12-29 18:22 +0000

csiph-web