This could be any statement evaluating to false.
Coffee script returns the result of the last statement. Javascript don't. So, failing to take this into account produces coffeescript that do not have the same behavior than the original javascript.
This is particularly important for iterators that return false for stopping iteration. Or for DOM event handlers that return false for stopping the propagation of the event.
You should expect this from a compiler, actually
> In my opinion, js functions should always return a value
Sounds wrong
> real code example of where your program would fail because of this?
Simple:
// That's perfectly fine; why would I return anything from this ?
link.onclick = function() {
doSomething();
};
// Now, translated in coffeescript:
link.onclick = ->
return doSomething(); // if this returns false, the "click" event is canceled function MyClass(x){
this.x = x;
}
In Coffeescript this translates to MyClass = (x) ->
@x = x
`x` is returned; and if you return something from a constructor, something is returned instead of the constructed object (`new MyClass({})` returns `{}`).Yeah, well, you know, that's just, like, your opinion, man.
It's perfectly valid for JavaScript functions to not return something. Your opinion about always returning something (which I agree with, btw) is only a personal stylistic preference, and from the code I've seen in the wild, it's not really that widespread.
As fooyc points out, the behavior of onClick events in the DOM changes depending on what return value is received. As I understand it, when an onClick executes a script, the truthiness of the value it returns determines whether the event continues to bubble up the DOM after the onClick has executed. At a guess, 75% of the onClick handler functions I've seen in the wild do not return a value; whether they realize it or not, all of those handlers implicitly rely on the appropriate truthiness of not returning a value for the rest of the page to behave correctly. Suddenly giving those functions a return value, especially one the developer didn't intentionally choose and didn't expect to have any effect, could cause quite a bit of unexpected behavior.
This also seems like an easy issue to address. No return statement in the JavaScript? Add an empty return in the CoffeeScript.