2016-09-20 4 views
-2

Whilest 워드 프로세서 읽기 : MDN Array forEach 나는 foreach 루프 내부의 인덱스를 얻으려고 노력하지만 그냥받지 못했습니다 ..자바 스크립트 foreach 루프 내부의 인덱스를 가져옵니다

var BANG = {}; 
 

 
BANG.boom = function (arr) { 
 
    this.array = arr; 
 
    
 
    this.start = function() { 
 
     Array.prototype.forEach.call(
 
      this.array, 
 
      (function (blubb, index) { 
 
       window.setInterval(
 
        this.hello(blubb, index), 
 
        1500 
 
       ); 
 
      }).bind(this) 
 
     ); 
 
    }; 
 

 
    this.hello = function(blubb, index) { 
 
     alert(blubb, index) 
 
    }; 
 
}; 
 

 
xxx = new BANG.boom(['xxx', 'yyy', 'zzz']); 
 
xxx.start();

어떻게 생각을 잘못하고있는거야?

+0

var BANG = {}; BANG.boom = function (arr) { this.array = arr; this.start = function() { Array.prototype.forEach.call( this.array, (function (blubb, index) { window.setInterval( this.hello(blubb, index), 1500 ); }).bind(this) ); }; this.hello = function(blubb, index) { alert(index+":"+blubb); // only one argument console.log(index,blubb); // console does understand the comma }; }; xxx = new BANG.boom(['xxx', 'yyy', 'zzz']); xxx.start();
는 "어떻게 내가 잘못 뭐하는 거지?" - for 루프를 사용하지 않음 –

+1

"그냥 가져 오지 않는다"는 것은 무엇을 의미합니까? – zero298

+0

네, ** ** 색인을 얻고 있습니다 : https://jsfiddle.net/9egp6qab/ 그냥 제대로 사용하지 않고 있습니다. [Mateusz의 대답] (http://stackoverflow.com/a/39595489/157247) 제대로 사용하는 방법을 보여줍니다. –

답변

2

작동하지만, alert은 단 하나의 인수 만 사용하므로 보이지 않습니다. :)