2014-12-16 7 views
-1

몇 가지 객체를 작성하고 관리하기 위해 생성자/프로토 타입 접근 방식을 사용하는 앱을 개발했습니다. JavaScript 및 jQuery를 사용하여 프로토 타입 중 하나는 익명 함수가 onclick 이벤트에 선언되는 입력 필드를 만듭니다.자바 스크립트 입력 이벤트 : retain() 루프 변수

익명 함수 내에서 외부 참조 (var = this)를 사용하여 기본 객체에 쉽게 액세스 할 수 있으며 입력 객체 자체에 연결된 속성에 액세스 할 수 있습니다. 그러나, 내 입력 필드는 for() 루프 내에서 빌드되며 루프 (유명한 "var i"를 사용하여 반복)에 의해 반복되는 반복 변수를 유지해야합니다.

"i"값이 참조로 전달되므로 모든 입력에 항상 마지막 값 "i"가 유지됩니다. 그래서 나는 입력 객체에 "i"속성을 부여함으로써 해결 방법을 발견했다.

제 질문은 이것이 좋은 접근 방법입니까? 아니면 다른 방법으로이 작업을해야합니까? ... 통해 반복 시도

대신 for()

// Let's create the constructor 
function familly(motherName,fatherName,children) { 
    // We give to the properties their values 
    this.motherName = motherName; 
    this.fatherName = fatherName; 
    this.children = children; 
} 

// Then we create a prototype that creates an input field for each child 
familly.prototype.createChildrenInputs = function() { 

    // I declare a variable that will serve as a reference to the main object (some people would name it "that") 
    var famillyObject = this; 

    // We pass into a loop all the children existing in the object property 
    var children = this.children; 
    for(var i in children) { 

     // We create the input field 
     var input = document.createElement('input'); 
      $(input).val(i); 

      // !!! WORKAROUND !!! : I attach the "i" variable as a property to the input 
      input.i = i; 

      $(input).on('click', function() { 

       // 1. The main object is accessible through the var famillyObject 
       console.log(famillyObject); 

       // 2. The value of the element is accessible with this.value 
       console.log(this.value); 


       // !!! HOWEVER !!! 
       // --------------- 

       // 3. The var "i" will always return "2" 
       console.log(i); 
       // 4. But the var "this.i" will show the good value (0, 1 or 2), since the reference was turned into a value with the workaround 
       console.log(this.i); 

      }); 
     document.body.appendChild(input); 
    } 
} 

var mother = 'Nancy'; 
var father = 'Eric'; 
var children = [ 
    { 
     'name':'Stephan', 
     'gender':'male' 
    }, 
    { 
     'name':'Lois', 
     'gender':'female' 
    }, 
    { 
     'name':'Andrew', 
     'gender':'male' 
    } 
]; 

var newFamilly = new familly(mother,father,children); 
newFamilly.createChildrenInputs(); 
+0

사용 바인드() 함수 랩, [] .MAP 대신 등을위한의 ... – dandavis

+0

나는 알렉시스 '좋아, 링크가 그가 나에게 대답처럼 외모를 넣어 생각합니다. 그러나, 나는 아직도 내 해결 방법은 좋은 해결책이 있는지 궁금해하고있어. 감사 ! –

+0

dandavis, 이러한 종류의 한 줄 대답은 "this.value"입력 매개 변수를 제거하지 않고 해당 특정 경우에 bind()를 사용하는 방법을 약간 상황화해야하기 때문에 나에게별로 도움이되지 않습니다. –

답변

0

이 항목은 잘 SO에 덮여 있지만 일부 주변 청소 장치도 가능하기 때문에 내가 대답을 제공하고 있습니다 : 여기

코드입니다 this.children.forEach(). 매 반복마다 함수 인수는 i을 비롯한 모든 로컬 변수를 포함하는 클로저를 형성합니다.

familly.prototype.createChildrenButtons = function() { 
    var famillyObject = this; 
    this.children.forEach(function(child, i) { 
     var $input = $('<input />').val(i).on('click', function() { 
      console.log(famillyObject); 
      console.log(this.value); 
      console.log(i); 
     }); 
     $(document).append($input); 
    }); 
}; 
+0

감사합니다 Roamer, 작동하는 것 같습니다. 나는 주제가 잘 다루어지고 있음을 의심 할 여지가 없다. 문제는 내가 대중적인 키워드와 관련하여 뭔가를 발견 할 수 없다는 것이다. for-loop, 변수, 범위 등. JavaScript와 SO에 익숙하지 않기 때문에 옳은 것을 목표로 삼을만한 모든 정확한 용어와 개념을 아는 것이 어렵습니다. 어쨌든, 설명 주셔서 감사합니다! –

+0

@dandavis, 질문에서 나는'this.children'이 컬렉션이 아니라 배열임을 이해합니다. 그렇다면'.forEach()'메소드가있다. '.apply()'할 필요가 없습니다. '이. 아이들 '이 소장품이라면,'이. 어린이들 (...)'이 갈 길이 될 것입니다. –

+0

@ Pierre-Luc, 동의 함, SO는 침투하기가 어렵습니다. 행운을 빕니다. –