2016-07-21 4 views
1

매번 값을 1 씩 증가 시키려고하는데 작동하지 않습니다. 내 변수 currentValue은 내가 0을 예상대로 제공하지만 var newValue = currentValue ++;을 사용하여 증가 시키려고해도 여전히 0이됩니다.자바 스크립트에서 업데이트 값이 증가하지 않습니다.

function updateClicks(e, maxValue) { 
    var buttonClicked = e.target; 
    addClass("clicked", buttonClicked); 
    var counterBox = buttonClicked.nextElementSibling; 

    var currentValue = buttonClicked.value; 
    console.log(currentValue); 

    var newValue = currentValue++; 
    console.log(newValue); 
} 

여기서 내가 뭘 잘못하고 있니?

+2

'NEWVALUE = CurrentValue에 ++'효과적으로 수행합니다'NEWVALUE = CurrentValue에; currentValue = currentValue + 1;'. 'newValue = ++ currentValue;'를 시도하거나,'currentValue'를 실제로 바꾸고 싶지 않다면'newValue = currentValue + 1' 만 시도하십시오. –

답변

2

당신은 당신이 pre increment 연산자처럼 사용할 필요가 증가 된 값에 영향을하려는 경우 : currentValue을 증가합니다

var newValue = ++currentValue; 

currentValue++ 이후 (포스트 증가를)하지만 newValue 변수에 할당 한 후.

  • pre-increment은 (++currentValue)의 뜻은 currentValue 다음 currentValue를 반환 값에 1을 추가합니다.
  • post-increment : (currentValue++)은 currentValue을 반환하고 그 중 하나를 더합니다.

희망이 도움이됩니다.

var currentValue = 0; 
 
console.log('Increment using pre increment : '+(++currentValue)); 
 
console.log('pre increment return : '+currentValue); 
 

 
console.log('-----------------------------------'); 
 

 
var currentValue = 0; 
 
console.log('Increment using post increment : '+(currentValue++)); 
 
console.log('post increment return : '+currentValue);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

0

시도 :

var newValue = ++currentValue; 
+0

약간의 설명이 도움이 될 것입니다. 문제는 무엇이며 어떻게 도움이 될까요? – showdev

+0

이 코드 단편은 질문을 해결할 수 있지만 [설명 포함] (// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) 정말 게시물의 품질을 향상시키는 데 도움이됩니다. 앞으로 독자의 질문에 답하고 있으며 코드 제안의 이유를 알지 못할 수도 있습니다. 코드와 설명의 가독성을 떨어 뜨리기 때문에 주석을 설명하는 코드를 사용하지 마십시오! – FrankerZ

2

그것은 처음으로 당신에게 공을 줄 것이다. 왜?
++ 연산자 - 값 을 반환하고 값을 증가시킵니다. 예

var a = 4; 
var b = a++; // Here it assigns 4 to b, then increments it's value 
console.log(a); // 5 
console.log(b); // 4 

당신은 pre increment 양식을 사용해야합니다

var a = 4; 
    var b = ++a; // Here it increments it's value then assigns it to b 
    console.log(a); // 5 
    console.log(b); // 5