Pass a list of named functions into function in coffeescript

断了今生、忘了曾经 提交于 2019-12-11 20:22:07

问题


In the API Documentation there is a snippet:

casper.waitFor(function check() {
    return this.evaluate(function() {
        return document.querySelectorAll('ul.your-list li').length > 2;
    });
}, function then() {
    this.captureSelector('yoursitelist.png', 'ul.your-list');
}, function timeout() {
    this.echo("I can't haz my screenshot.").exit();
});

I need this, but in a coffeescript project. I tried to rewrite it into coffeescript but it didn’t work. Even if i let js2coffe do the job, i get some invalid coffeescript from valid javascript:

i dont know how to pass a list of named functions into another function correctly.


回答1:


CoffeeScript doesn't really support named functions like that, see:

  • How do I create a named function expressions in CoffeeScript?
  • Function declaration in CoffeeScript

That specific example doesn't need them anyway, it looks like they're just there for documentation purposes so you could write:

check    = -> @evaluate(-> document.querySelectorAll('ul.your-list li').length > 2)
and_then = -> @captureSelector('yoursitelist.png', 'ul.your-list')
timeout  = -> @echo("I can't haz my screenshot.").exit()
casper.waitFor(check, and_then, timeout)

in CoffeeScript to get the same effect.



来源:https://stackoverflow.com/questions/27131288/pass-a-list-of-named-functions-into-function-in-coffeescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!