Comma operator in Arrow functions
ConciseBody is an AssignmentExpression in this case, so I believe the comma is a syntax error.
Yep, which is why it parses it as (a=()=>1),2 as far as I can see, so "1"
is expected indeed
To me, a=()=>1,2;
looks like statement1,statement2
where statement1
is the arrow function, and statement2
is 2
.
On Mar 14, 2015, at 12:37 PM, Caitlin Potter wrote:
ConciseBody is an AssignmentExpression in this case, so I believe the comma is a syntax error.
no, comma is the lowest precedence of any operator.
hence:
a=1,2
parses as:
(a=1),2
and
a=()=>1,2
parses as:
(a=()=>1),2
Finally, note that
console.log(a=1,2)
assigns the value 1 to a, but outputs 2
no, comma is the lowest precedence of any operator.
I think you might have misunderstood what I was saying?
ConciseBody -> AssignmentExpression -> comma is not a part of the ConciseBody -> you probably get a SyntaxError unless the arrow function is passed as a parameter to a function
So: var a = (x) => 1, 2;
-> syntax error (not a VariableDeclaration)
Is all I was saying.
So:
var a = (x) => 1, 2;
-> syntax error (not a VariableDeclaration)Is all I was saying.
hence:
a=1,2
parses as:
(a=1),2
and
a=()=>1,2
parses as:
(a=()=>1),2
Finally, note that
console.log(a=1,2)
assigns the value 1 to a, but outputs 2
Allen
Looks like I can agree with above, b But why in case of
x=1,2;
assigns 2 to x?
Opps...
Looks like I can agree with above, b But why in case of
x=1,2;
assigns 2 to x?
Error, it is assigning 1, ignore previous mail.
On Mar 14, 2015, at 1:00 PM, Caitlin Potter wrote:
no, comma is the lowest precedence of any operator.
I think you might have misunderstood what I was saying?
ConciseBody -> AssignmentExpression -> comma is not a part of the ConciseBody -> you probably get a SyntaxError unless the arrow function is passed as a parameter to a function
So:
var a = (x) => 1, 2;
-> syntax error (not a VariableDeclaration)
agreed, because the RHS of an Initializer must be an AssignmentStatement.
On Sun, Mar 15, 2015 at 5:52 AM, Allen Wirfs-Brock <allen at wirfs-brock.com>
wrote:
Finally, note that
console.log(a=1,2)
assigns the value 1 to a, but outputs 2
I think you meant to type console.log((a=1,2))
?
An HTML attachment was scrubbed... URL: esdiscuss/attachments/20150314/c7b33703/attachment
I was looking into Firefox implementation of Arrow functions
And noticed this
function a(){return 1,2}; a();
gives "2"
But with same with Arrow functions
a=()=>1,2;
a();
gives "1"
To get answer "2" in Arrow functions you need parentheses, like
a=()=>(1,2);
a();
Is this expected ?