3

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?

2
  • 1
    Can't help you much with CoffeeScript, but: "but I can't seem to get the null return" In JavaScript, return; returns undefined not null. Commented May 21, 2012 at 19:28
  • what are you returning from? Why not return null? Commented May 21, 2012 at 19:48

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.