2016-08-17 46 views
-1

同時學習如何調試我需要在瀏覽器中看到保存到變量中的操作的結果。Console.log將函數集的結果寫入變量

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

this.slider有以下幾點: enter image description here

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

enter image description here

我想收到作出內部this.slider變化res函數,所以我可以比較原始值和新值。

+0

這有什麼錯'前和手術後輸出'的console.log(this.slider)? –

+0

沒有什麼錯,我只是想學習如何在瀏覽器中使用console.log,以便我可以學習調試。我只是試圖顯示之前,然後在Res內進行的操作。 – FerchoCarcho

回答

0

字面上只需登錄之前和之後操縱你的對象的代碼:

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); 
0

res是一個函數。您需要單獨定義函數,然後調用它並將結果保存在res中,如果您希望它在那裏。

例如,

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

res = example(); 
console.log(res); 

而且,你的問題是非常難以閱讀。

編輯:你的問題很難解讀,我顯然不理解它,我不認爲這是你正在尋找的答案。請使用代碼片段而不是圖片重新格式化。

編輯2:安德烈Dion是正確的,這仍然會記錄'未定義',因爲你沒有從函數返回任何東西。

+0

這仍然會返回'undefined',因爲它沒有顯式地有'return'語句,因爲'Array.prototype.forEach'也返回'undefined'。 –

+0

是的,我只是顯示海報是控制檯登錄功能本身,而不是它會返回的任何結果 – BIU

+0

我試圖讓它更清晰。感謝您回覆 – FerchoCarcho