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

Javascript 如何在forEach的回调中回显数组元素,javascript,casperjs,Javascript,Casperjs,我想echo一组链接的内容。尝试使用以下方法: casper.then(function() { // aggregate results for the 'casperjs' search links = this.evaluate(getLinks); links.forEach(function (element, index, array) { echo(element); }); }); 但是得到一个错误: TypeError: 'un

我想
echo
一组链接的内容。尝试使用以下方法:

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)')
如何将每个链接回显到控制台?

更换

echo(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);
    });
});

我担心这是个棘手的问题。您在哪里定义了
echo
函数?错误消息和您的代码不匹配。请考虑我是Javascript初学者。我在虚拟终端上运行这个<代码>控制台。日志不会在终端上回显任何内容。请编辑您的问题以更改代码或错误消息,因为它们不匹配。代码中没有
this.echo(…)
,但只有
echo(…)
可能重复的
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);
    });
});