如何在 forEach 的回调中回显数组元素

How to echo elements of array in a callback for a forEach

我想echo link 数组的内容。尝试使用这个:

casper.then(function() {
    // aggregate results for the 'casperjs' search
    links = this.evaluate(getLinks);
    links.forEach(function (element, index, array) {
        echo(element);
    });
});

但是报错:

TypeError: 'undefined' is not a function (evaluating 'this.echo(element)')

如何将每个 link 回显到控制台?

替换

echo(element);

console.log(element);

根据文档,它是 "this.echo";但是,由于 "this" 可能会根据上下文发生变化,因此您需要保存父上下文:

casper.then(function() {
    var self=this;
    // aggregate results for the 'casperjs' search
    links = this.evaluate(getLinks);
    links.forEach(function (element, index, array) {
        self.echo(element);
    });
});

http://docs.casperjs.org/en/1.1-beta2/modules/casper.html