Breck
Breck

Reputation: 690

Putting Lambdas in OR statement

Can someone explain why the second example does not work:

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

Upvotes: 0

Views: 55

Answers (2)

Ryan Cavanaugh
Ryan Cavanaugh

Reputation: 220994

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.

Upvotes: 3

frhack
frhack

Reputation: 5112

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

Upvotes: 1

Related Questions