如何使用Coffeescript的params写setTimeout [英] how to write setTimeout with params by Coffeescript

查看:170
本文介绍了如何使用Coffeescript的params写setTimeout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我如何在coffeescript中编写javascript。

Please tell me how to write javascript below in coffeescript.

setTimeout(function(){
    something(param);
}, 1000);


推荐答案

我认为这是一个有用的回调函数的最后一个参数。例如,这通常是Node.js API的情况。所以考虑到这一点:

I think it's a useful convention for callbacks to come as the last argument to a function. This is usually the case with the Node.js API, for instance. So with that in mind:

delay = (ms, func) -> setTimeout func, ms

delay 1000, -> something param

授予,这增加了额外的函数调用每个 setTimeout 你make;但在今天的JS解释器中,性能上的缺点是微不足道的,除非你每秒钟做数千次。 (你在做什么设置每秒数以千计的超时?)

Granted, this adds the overhead of an extra function call to every setTimeout you make; but in today's JS interpreters, the performance drawback is insignificant unless you're doing it thousands of times per second. (And what are you doing setting thousands of timeouts per second, anyway?)

当然,一个更直接的方法是简单地命名回调,这往往产生更多(jashkenas是这个成语的大粉丝):

Of course, a more straightforward approach is to simply name your callback, which tends to produce more readable code anyway (jashkenas is a big fan of this idiom):

callback = -> something param
setTimeout callback, 1000

这篇关于如何使用Coffeescript的params写setTimeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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