将参数传递给 CasperJS 的 evaluate 函数

Passing parameters to function in CasperJS's evaluate

如何在 CasperJS 的求值中将参数传递给函数?

    //Should be logged in at this point
    casper.then(function() {
        var counter = 0;
        var cap = 500;

        this.evaluate(function(counter) {
            var children = $('.companies-using-service').children();

            while (counter < children.length) {
                child = children[counter];
                console.log($(child).find('a').attr('data-hint'));
                counter++;
            }
        }, counter);
    });
};

var scrapeClients = function(counter) {
    var children = $('.companies-using-service').children();

    while (counter < children.length) {
        child = children[counter];
        console.log($(child).find('a').attr('data-hint'));
        counter++;
    }
}

以上,我可以使用未命名函数传递参数。但是,我希望将函数 scrapeClients 传递给评估函数。在那种情况下,我尝试了以下 this.evaluate(scrapeClients(counter), counter)。但是,这不起作用,错误提示找不到 $ 变量。

函数是 JavaScript 中的第一个 class 公民。您可以像对待变量一样对待它们。你可以传递它们。这意味着你不想

this.evaluate(scrapeClients(counter), counter)

而是

this.evaluate(scrapeClients, counter)

在第一种情况下,您实际上是在直接调用该函数。由于该函数使用了一些仅在 casper.evaluate 内部可用的页面属性,这将引发错误并停止脚本。