最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

syntax - Javascript - What does (1, 2) actually mean? - Stack Overflow

programmeradmin4浏览0评论

I know that in javascript the syntax (1, 'a', 5, ... 'b') will always return the last value but what does this syntax actually mean? When I see (1, 2) -- which, admittedly is pretty much never -- how should I parse that syntax?

I know that in javascript the syntax (1, 'a', 5, ... 'b') will always return the last value but what does this syntax actually mean? When I see (1, 2) -- which, admittedly is pretty much never -- how should I parse that syntax?

Share Improve this question asked Aug 23, 2012 at 21:51 George MauerGeorge Mauer 122k140 gold badges396 silver badges630 bronze badges 5
  • ... Actual arguments being passed to a method/function? (Are you confusing this with boolean expressions like 1 && 'a' && 5 && ... 'b'?) – EthanB Commented Aug 23, 2012 at 21:54
  • 3 Mozilla documentation for the ma operator. – Snowball Commented Aug 23, 2012 at 21:55
  • @Snowball: You should post that as an answer. – Sasha Chedygov Commented Aug 23, 2012 at 21:55
  • @EthanB - nope. Try it in the console, it works. Surprised the crap out of me the first time too :) – George Mauer Commented Aug 23, 2012 at 22:00
  • Wow. I just learned something new.... – EthanB Commented Aug 23, 2012 at 22:01
Add a ment  | 

5 Answers 5

Reset to default 5

The individual expressions - the bits between the mas - are evaluated, then the overall expression takes the value of the last one. So listing a series of numeric and string literals like your example is kind of pointless:

(1, 'a', 5, ... 'b')
// does the same thing as
('b')

...so you might as well leave out all but the last. However if the individual expressions have other effects because they are function calls or assignments then you can't leave them out.

About the only good reason I can think of to use this syntax is within a for statement, because for syntax:

for([init]; [condition]; [final-expression])

...doesn't allow you to use semicolons within the [init] part, or within the [condition] or [final-expression] parts. But you can include multiple expressions using mas:

for(x = 0, y = 100, z = 1000; x < y; x++, y--, z-=100) { ... }

The , operator evaluates both its arguments and returns the last one.

It is most monly used in for loops:

for( i=0, j=0; ...)

MDN docs

The ma operator evaluates each item, then returns the last one:

var x = (1, 2, 3);
console.log(x == 3);  // true

Here's Mozilla's documentation.

That's just the ma operator, which evaluates two expression and returns the second one. Since it evaluates arguments from left to right, if you have a list of arguments separated by mas the last one will be returned. From The Elements of JavaScript Style:

The ma operator was borrowed, like much of JavaScript's syntax, from C. The ma operator takes two values and returns the second one. Its presence in the language definition tends to mask certain coding errors, so pilers tend to be blind to some mistakes. It is best to avoid the ma operator, and use the semicolon statement separator instead.

Parentheses - is just a priority changing operator. So in this case you may omit them.

And you leave with 1, 2, which is just two expressions separated by ma.

发布评论

评论列表(0)

  1. 暂无评论