coffeescript总是返回 [英] coffeescript always returns

查看:115
本文介绍了coffeescript总是返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个可能是一个愚蠢的问题,关于咖啡脚本。我给它第二次机会,但为什么它会返回 所有



是函数的最后一个语句/行?如何禁用?把评论或某事作为最后的表达,我知道这是一个记录功能,但没有;不是真的,我如何到处 返回并保存下载/执行时间?



当然,这种行为类似于jit的过程?

 (locate = 
getPosition: ) - >
#检查我们是否支持geolocation
throw Exception'您的浏览器不支持基于位置的服务!'如果!navigator.geolocation

navigator.geolocation。 getCurrentPosition(pos) - >
console.log pos

 (function(){
var locate;

locate = {
getPosition:function(){
if(!navigator.geolocation){
throw Exception('您的浏览器不支持基于位置的服务!');
}
return navigator.geolocation.getCurrentPosition(function(pos){
return console.log(pos);
});
}
};

})。call(this);



b

我关心的原因是这是一个非常大的库中的一个应用程序,我已经建立,如果我们说有500个函数,其中200个做一些事情dom,而不是返回类似数字或对象,额外的200返回是额外的1.2k的数据我不想要或需要。



此外,一个没有返回的函数,返回 undefined 和一个返回 null 的函数,不需要解释。如果我很蠢,可以检查这一点,这是错误的。。



我确定会有一些perf差异,但我不知道现在我没有时间做一些jsperfs,但我会感兴趣。



解决方案

是coffeescript总是返回最后一行功能。它可以这样做,因为coffeescript中的一切都是一个表达式。



docs


一切皆为表达式(至少尽可能多)



您可能已经注意到即使我们不向CoffeeScript函数添加return语句,它们仍然返回它们的最终值。 CoffeeScript编译器尝试确保语言中的所有语句都可以用作表达式。


他们的例子可以看到此处



您仍然可以使用 return 语句


即使函数总是返回它们的最终值,一个函数体,当你知道你已经完成时,写出显式返回值(返回值)。



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.

解决方案

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.

这篇关于coffeescript总是返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆