Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 831
onlineracoon Avatar asked May 13 '26 03:05

onlineracoon


2 Answers

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.

like image 114
numbers1311407 Avatar answered May 14 '26 17:05

numbers1311407


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.

like image 41
Robert Levy Avatar answered May 14 '26 16:05

Robert Levy