将命名函数的列表传递给coffeescript的函数 [英] Pass a list of named functions into function in coffeescript

查看:228
本文介绍了将命名函数的列表传递给coffeescript的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在API文档中有一段代码:

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

我需要这个,但是在coffeescript项目中。我试图重写它到coffeescript,但它没有工作。即使我让

  • CoffeeScript中的函数声明



  • 这个特定的例子不需要他们,看起来像他们只是为了文档目的,写:

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

    在CoffeeScript中获得相同的效果。


    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.

    解决方案

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

    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.

    这篇关于将命名函数的列表传递给coffeescript的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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