admin管理员组文章数量:1401783
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?
-
... 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
5 Answers
Reset to default 5The 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.
本文标签: syntaxJavascriptWhat does (12) actually meanStack Overflow
版权声明:本文标题:syntax - Javascript - What does (1, 2) actually mean? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744317271a2600325.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论