Coffeescript return

I have this javascript piece:

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

And I tried converting it to Coffeescript, but I can't get a null return to work:

if @isShown or event.isDefaultPrevented()
    return;

How can I make it work correctly?

+3
source share
2 answers

It appears that the CoffeeScript compiler will not implicitly return null unless it is required to prevent subsequent code execution. If something happens after this code, it will add a null return, for example:

if @isShown or event.isDefaultPrevented()
  return

alert(1)

// compiles to =>

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

alert(1);

While in your case above the function will just exit anyway after the conditional expression without making an unnecessary return.

+2
source

... http://coffeescript.org, "" CS . JS JS-.

CoffeeScript , . , ... , ! , , .

+1

All Articles