2017-12-22 3 views
0

함수의 본문에서 외부 범위의 this을 볼 수있는 경우 각 화살표 함수에 대해 domMatch를 내부에 표시하고 싶습니다. 내 범위 전체에 클래스가 있어야합니다. domMatch 객체를 전달하는 방법.foreach 함수에 외부 범위 변수를 전달하는 방법

  let tr = document.createElement("tr"); 

      let domMatch = {}; 

      domMatch.status = this.status; 
      ... other props 


      Object.getOwnPropertyNames(domMatch).forEach(function (domMatchField, a, b, domMatch) { // domMatch is undefined 
       let cell = tr.insertCell(-1); 

       cell.innerText = domMatch[domMatchField]; 

      }); 
+2

'domMatch'라는 매개 변수를 추가하지 마십시오, 당신은 그냥 – user184994

+0

그냥 (!) 참고 액세스 할 수 있어야합니다 - 당신은 당신의 코드에있는 화살표 기능을 가지고 있겠지. – Jamiec

+0

@Jamiec 더 이상, 고마워, 내가 q를 업데이 트했습니다 – Aubergine

답변

2

문제는 당신이 forEach에 콜백 함수를 통해 변수 domMatch를 전달하는 데 노력하고 있다는 것입니다 - 물론 네 번째 undefined

그냥 생략 할 수 있도록이 기능은 3 개 인자 (대부분에서) 받아 해당 인수 및 내부 범위 내에서 domMatch을 사용할 수 있어야합니다.

var domMatch = {}; 
 
domMatch.status = "foo"; 
 

 
Object.getOwnPropertyNames(domMatch).forEach(function(key, index, arr){ 
 
    console.log("iterating over key:", key); 
 
    console.log("index:",index); 
 
    console.log("Original array:", arr); 
 
    // Now the important bit! 
 
    console.log("domMatch", domMatch); 
 
});

+0

+, 고마워, 작품 Logged – Aubergine

관련 문제