Comma operator in Arrow functions

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Comma operator in Arrow functions

Biju Gm@il
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 ?



Cheers
GC
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Comma operator in Arrow functions

Caitlin Potter
ConciseBody is an AssignmentExpression in this case, so I believe the comma is a syntax error.



> On Mar 14, 2015, at 3:29 PM, Biju <[hidden email]> wrote:
>
> 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 ?
>
>
>
> Cheers
> GC
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Comma operator in Arrow functions

kangax
Yep, which is why it parses it as (a=()=>1),2 as far as I can see, so "1" is expected indeed

-- 
kangax

On Sat, Mar 14, 2015 at 8:37 PM, Caitlin Potter <[hidden email]> wrote:
ConciseBody is an AssignmentExpression in this case, so I believe the comma is a syntax error.



> On Mar 14, 2015, at 3:29 PM, Biju <[hidden email]> wrote:
>
> 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 ?
>
>
>
> Cheers
> GC
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Comma operator in Arrow functions

Jordan Harband
In reply to this post by Caitlin Potter
To me, `a=()=>1,2;` looks like `statement1,statement2` where `statement1` is the arrow function, and `statement2` is `2`.

On Sat, Mar 14, 2015 at 12:37 PM, Caitlin Potter <[hidden email]> wrote:
ConciseBody is an AssignmentExpression in this case, so I believe the comma is a syntax error.



> On Mar 14, 2015, at 3:29 PM, Biju <[hidden email]> wrote:
>
> 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 ?
>
>
>
> Cheers
> GC
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Comma operator in Arrow functions

Allen Wirfs-Brock
In reply to this post by Caitlin Potter

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

Allen


>
>
>
>> On Mar 14, 2015, at 3:29 PM, Biju <[hidden email]> wrote:
>>
>> 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 ?
>>
>>
>>
>> Cheers
>> GC
>> _______________________________________________
>> es-discuss mailing list
>> [hidden email]
>> https://mail.mozilla.org/listinfo/es-discuss
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
>

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Comma operator in Arrow functions

Caitlin Potter
> 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.

> On Mar 14, 2015, at 3:52 PM, Allen Wirfs-Brock <[hidden email]> wrote:
>
>
> 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
>
> Allen
>
>
>>
>>
>>
>>> On Mar 14, 2015, at 3:29 PM, Biju <[hidden email]> wrote:
>>>
>>> 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 ?
>>>
>>>
>>>
>>> Cheers
>>> GC
>>> _______________________________________________
>>> es-discuss mailing list
>>> [hidden email]
>>> https://mail.mozilla.org/listinfo/es-discuss
>>
>> _______________________________________________
>> es-discuss mailing list
>> [hidden email]
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Comma operator in Arrow functions

Biju Gm@il
> 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?
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Fwd: Comma operator in Arrow functions

Biju Gm@il
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.

Cheers
GC
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Comma operator in Arrow functions

Allen Wirfs-Brock
In reply to this post by Caitlin Potter

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.

Allen
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Comma operator in Arrow functions

Leon Arnott
In reply to this post by Allen Wirfs-Brock
On Sun, Mar 15, 2015 at 5:52 AM, Allen Wirfs-Brock <[hidden email]> 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))` ?

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Comma operator in Arrow functions

Allen Wirfs-Brock
In reply to this post by Biju Gm@il


On Mar 14, 2015 6:53 PM, Leon Arnott <[hidden email]> wrote:
>
> On Sun, Mar 15, 2015 at 5:52 AM, Allen Wirfs-Brock <[hidden email]> 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))` ?

Sure enough.


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss