2012-07-28 43 views
0

不幸的是,下面的代碼不會在螢火蟲控制檯上打印任何內容,它也不會拋出錯誤,不知道這段代碼有什麼問題。試圖讓代碼使用javascript調用工作

$(document).ready(function(){ 

var animals = [ 
     {species: 'Lion', name: 'King'}, 
     {species: 'Whale', name: 'Fail'} 
    ]; 

    for (var i = 0; i < animals.length; i++) { 
     (function (i) { 
     this.print = function() { 
      console.log('#' + i + ' ' + this.species + ': ' + this.name); 
     } 
     }).call(animals[i], i); 
    } 

}); 
+2

你在任何時候都不會調用'this.print()'。 – JJJ 2012-07-28 06:15:36

回答

1

您正在分配.print()方法將animals陣列中每個對象,但你永遠不會調用該方法,所以從這個代碼沒有預期的輸出。