2012-03-06 3 views

답변

2

this은 문자열이며 로컬 변수에 대한 참조가 아닙니다. 문자열을 늘려서 변수를 늘릴 수는 없습니다. 또한 로컬 변수를 문자열로 주어진 이름으로 참조하려고합니다. 로컬 변수를 사용하는 경우 eval을 통해서만 또는 변수 이름을 지정하여이 작업을 수행 할 수 있습니다.

이 작동합니다 :

var ns = { 
    MidUpperArmCircumference: 0, 
    TricepsSkinfold: 0 
}; 

function checkMethod(method,parameters){ 
    $('#'+method+'_check').change(function() { 
     if (this.checked == true) { 
      $.each(parameters, function() { 
       $('.'+this).css('color','blue'); 
       ns[this]++; // <-- Fixed. 
      }); 
     } 
    }); 
} 
+0

*하지 항상 * "'this'는 객체 참조입니다." strict 모드에서는 * thisArg *로 설정된 경우 프리미티브가됩니다. 여기서는 해당됩니다. * "개체를 늘릴 수 없습니다."* Number 개체 또는 값을 * toNumber *로 변환 할 수있는 개체가 아닌 한 (해당 개체는 프리미티브로 강제 변환됩니다). –

+1

@amnotiam 어, 그건 참으로 쓰레기였습니다. 나는 그 부분을 (질문과 관련하여) 바꿨다. –

+0

일했습니다! 너무 많이! – Munir

0

쉬운, 그냥 ++그들에게 :

function checkMethod(method,parameters){ 
    $('#'+method+'_check').change(function() { 
    if (this.checked == true) { 
     $.each(parameters, function() { 
      TricepsSkinfold++; 
      MidUpperArmCircumference++; 
      ... 
      ... 
      $('.'+this).css('color','blue'); // That looks wrong as well... 
      // as "this" is the current item of the iteration. 
     }); 
    } 
    }); 
} 
+0

이것은 내게 더 많은 질문이다. 변수가'$ (document) .ready()'블록 안에 선언 될 때 여전히 동작 할까? – ManseUK

+0

@ManseUK. 예, 시도해 볼 수 있습니다. – gdoron

+0

제가 생각하기에 :-) – ManseUK