Javascript 将函数集的结果记录到变量中

Javascript 将函数集的结果记录到变量中,javascript,debugging,Javascript,Debugging,在学习如何调试的同时,我需要在浏览器中查看保存到变量中的操作结果 var res= function() { [].forEach.call(this.slider, function(el) { return el.className = 'item'; })}.bind(this); 此.slider具有以下功能: 如果Iconsole.log(res) 我得到: 我想在res函数中接收对此.sli

在学习如何调试的同时,我需要在浏览器中查看保存到变量中的操作结果

 var res=    function() {
            [].forEach.call(this.slider, function(el) {
                return el.className = 'item';
            })}.bind(this);
此.slider具有以下功能:

如果I
console.log(res)
我得到:


我想在res函数中接收对此.slider所做的更改,以便比较原始值和新值。

res
是一个函数。您需要单独定义函数,然后调用它并将结果保存在
res
中(如果您希望它在那里)

比如说,

function example() {
        [].forEach.call(this.slider, function(el) {
            return el.className = 'item';
        })}.bind(this);

res = example();
console.log(res);
另外,你的问题很难理解

编辑:你的问题很难理解,我显然不明白,我认为这不是你想要的答案。请使用代码段而不是图像重新格式化


编辑2:Andre Dion是正确的,这仍然会记录“未定义”,因为您没有从函数返回任何内容

只需在操作对象的代码前后记录:

var res = function() {
    console.log('before:', this.slider);

    [].forEach.call(this.slider, function(el) {
        return el.className = 'item';
    });

    console.log('after:', this.slider);
}.bind(this);

这仍然会返回
undefined
,因为它没有显式的
return
语句,而且因为
Array.prototype.forEach
也会返回
undefined
。是的,我刚才展示的海报是控制台记录函数本身,而不是它将返回的任何结果。我试图让它更清楚。感谢您的回复在操作前后输出
console.log(this.slider)
有什么问题吗?没有什么问题,我只是想学习如何在浏览器中输入console.log,以便学会调试。我只是想在Res中显示操作的前后。