Coffeescript always comes back

So, I probably have a stupid question to ask about Coffee Script. I give him a second chance, but why does he return everything ?

What does the last statement / line of the function have to do with? and how do i disable this? Assuming a comment or something as the final "expression", I know that this is a "documented" function, but not; no, it’s not so, how can I not return everywhere? and save load / run time?

Is this behavior like jit over screws?

(locate = getPosition: () -> # Check we support geolocation throw Exception 'Your browser doesn\'t support location based services!' if !navigator.geolocation navigator.geolocation.getCurrentPosition (pos) -> console.log pos ) 

Compiles

 (function() { var locate; locate = { getPosition: function() { if (!navigator.geolocation) { throw Exception('Your browser doesn\'t support location based services!'); } return navigator.geolocation.getCurrentPosition(function(pos) { return console.log(pos); }); } }; }).call(this); 

[change]

The reason I care is just one of the very large library for the application that I built if we say that 500 functions and 200 of them do something for dom instead of returning something like a number or object that an additional 200 returns is an additional 1.2 thousand data that I do not want or need.

In addition, the function without returning returns undefined and the function that returns null well, no need to explain it. If I were stupid enough to test this, it would be wrong in all directions.

I am sure that there will be some significant differences, but I do not know about it, and now I do not have time to execute some jsperfs, but I would be interested.

Undefined and null

+2
source share
2 answers

Yes coffeescript will always return the last line of a function. This can be done since everything in coffeescript is an expression.

From docs :

Everything is an expression (at least as much as possible)

You may have noticed that although we do not add return statements to CoffeeScript functions, they nonetheless return their final value. The CoffeeScript compiler tries to make sure that all expressions in the language can be used as expressions. See how the return falls into every possible execution branch in the function below.

Their example can be seen here.

You can still get a fault return using the return

Despite the fact that functions always return their final value, it is possible and recommended to return early from the body of the function, writing out an explicit return (return value) when you know that everything is done.

+5
source

This is because this is the last statement / line of the function, yes. By default, functions in CoffeeScript always return a value . This is usually not bad, but you can just add the return line if you really don't want to return anything.

If you want to return something specific, you can simply make this last line of your function:

 (locate = getPosition: () -> # Check we support geolocation throw Exception 'Your browser doesn\'t support location based services!' if !navigator.geolocation navigator.geolocation.getCurrentPosition (pos) -> console.log pos 'Return this string' return ) 

JS:

 var locate; locate = { getPosition: function() { if (!navigator.geolocation) { throw Exception('Your browser doesn\'t support location based services!'); } navigator.geolocation.getCurrentPosition(function(pos) { console.log(pos); return 'Return this string'; }); } }; 
0
source

All Articles