onlineracoon
onlineracoon

Reputation: 2970

coffeescript return

I have this piece of javascript:

if(this.isShown || event.isDefaultPrevented()){
    return;
}

And I tried to convert it into Coffeescript but I can't seem to get the null return to work:

if @isShown or event.isDefaultPrevented()
    return;

How can I get it working properly?

Upvotes: 3

Views: 478

Answers (2)

numbers1311407
numbers1311407

Reputation: 34072

It appears that the CoffeeScript compiler won't implicitly return null unless it needs to to prevent later code from executing. If something happend after that code, it would add the null return, e.g.:

if @isShown or event.isDefaultPrevented()
  return

alert(1)

// compiles to =>

if (this.isShown || event.isDefaultPrevented()) {
  return;
}

alert(1);

Whereas in your case above, the function would just exit anyway after the conditional, rendering a null return unnecessary.

Upvotes: 2

Robert Levy
Robert Levy

Reputation: 29073

You're problem must lie elsewhere... I went to http://coffeescript.org, clicked 'try it', and pasted in your CS code. The JS it generated matches your original JS code.

CoffeeScript error messages often don't actually mean what they say. That's what makes it so much fun... you get be a detective but without having to quit your job as a programmer! Post a larger code block and maybe we can help you with your detective work.

Upvotes: 1

Related Questions