Putting Lambdas in OR statement

106 Views Asked by At

Can someone explain why the second example does not work:

var thisWorks = true || function () {};
var thisBreaks = true || () => {};
2

There are 2 best solutions below

0
On BEST ANSWER

This is how the precedence of the various operators in ECMAScript 6 works.

There's a great explanation at http://typescript.codeplex.com/workitem/2360 that walks through each production in sequence.

0
On

use:

 var thisBreaks = true || (() => {});

I think is related to operators priority.

var thisBreaks = true || (()=>{ }) ;

compile to javascript:

var thisBreaks = true || (function () { });

while

var thisBreaks = true || ()=>{};

compile to javascript:

var thisBreaks = true || ();
{ }
;

Try yourself here: http://www.typescriptlang.org/Playground