Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

coffeescript not implicitly returning "false"

Tags:

coffeescript

I am trying to add a method to the String primitive type/class where I can extra params from a URL string

String::getUrlParams = ->                        # line 1 
    false unless ( params = @.split('?')[1] )    # line 2
    # ...

In Chrome console, when I deliberately call this method with a string of URL without params, I expect it to just return false.

"http://dns.com/".getUrlParams();

but it goes pass through line 2.

if I change line 2 to

   return false unless ( params = @.split('?')[1] ) # line 2`

then it does return false and stops the function at line 2

Any idea why coffeescript isn't returning false and halts the function in the first version?

Thank you

like image 512
Nik So Avatar asked May 03 '26 14:05

Nik So


1 Answers

Coffeescript returns only the last function statement. If something follows some statement, that you want to return in middle of function, then you should do that explicitly.

--Short thoughts-- In short - Coffeescript compiler is not that smart, to predict, where you may want or may not want to return something. And same applies to most compilers now days. Also it's non-smartiness avoids most of mistakes, which would be caused because of premature return.

like image 73
nagisa Avatar answered May 05 '26 12:05

nagisa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!