coffeescript always returns

2019-07-09 04:13发布

问题:

So I have what's probably a stupid question to ask about Coffee Script. I'm giving it a second chance but why does it return everything?

Is it anything to do with being the last statement/line of the function? and how do I disable this? Putting a comment or something as the final "expression", I know it's a "documented" feature but no; no it's not really, how do I not have returns everywhere? and save download/execution times?

Surely this behaviour kind of screws the jit over?

(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 to

(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);

[Edit]

The reason I care is this is just one of a very large library for an app I've built, if we say there's 500 functions and 200 of those do something to the dom instead of return something like a number or object, that extra 200 returns is an extra 1.2k of data I don't want or need.

Also, a function with no return, returns undefined and a function that returns null well, no need to explain that. If I was stupid enough to check this, it'd be wrong across the board.

I'm sure there would be some perf differences but I don't know about that and right now I don't have time to do some jsperfs but I'd be interested.

回答1:

Yes coffeescript will always return the last line of the function. It can do this since everything in coffeescript is an expression.

From the docs:

Everything is an Expression (at least, as much as possible)

You might have noticed how even though we don't add return statements to CoffeeScript functions, they nonetheless return their final value. The CoffeeScript compiler tries to make sure that all statements in the language can be used as expressions. Watch how the return gets pushed down into each possible branch of execution in the function below.

Their example can be seen here

You can still do short-circuit returns with the return statement

Even though functions will always return their final value, it's both possible and encouraged to return early from a function body writing out the explicit return (return value), when you know that you're done.



回答2:

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

If you want to return something specific, you can just make that the 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';
    });
  }
};