2017-03-03 7 views
0

これは簡単な電卓練習の部分的なコードです:誰かが私に説明することができます!このjqueryのlockButton

私の理解から!ブール値を反転するので、lockButtonsはfalseに設定されているため、最初のif文でtrueになりますか?しかし、後でif check =の下で、lockButtonsをtrueに設定して、数字が入力されなくなるようにしましたが、おそらく単純ですが、私の周りを頭で囲むことはできません。

var firstNumber = ""; 
 
     var secondNumber = ""; 
 
     var operator = ""; 
 
     var result = 0; 
 
     var hasNumber = false; 
 
     var firstNumberComplete = false; 
 
     var lockButtons = false; 
 

 
     // Check if any button is clicked... 
 
     $(document).on("click", "button", function() { 
 

 
     // Checks if it's a number and that its not the end of the calculation ("!lockButtons") 
 
     if ($(this).hasClass("number") && !lockButtons) { 
 

 
      // We'll then set our "hasNumber" variable to true to indicate that we can proceed in selecting an operator. 
 
      hasNumber = true; 
 

 
      // If we haven't received an operator yet... 
 
      if (firstNumberComplete === false) { 
 

 
      // Then grab the number of the value clicked and build a string with it 
 
      firstNumber += $(this).attr("value"); 
 

 
      // Print the number to the firstPage 
 
      console.log(firstNumber); 
 

 
      // Print it to the div 
 
      $("#first-number").html(firstNumber); 
 
      } 
 

 
      // If we have received an operator already... 
 
      else { 
 

 
      // Grab the number of the value clicked and build a string with it 
 
      secondNumber += $(this).attr("value"); 
 

 
      // Print the number to the firstPage 
 
      console.log(secondNumber); 
 

 
      // Print it to the div 
 
      $("#second-number").html(secondNumber); 
 
      } 
 
     } 
 

 
     // Checks if its an operator (but not "=") 
 
     if ($(this).hasClass("operator") && hasNumber && !lockButtons) { 
 
      firstNumberComplete = true; 
 

 
      // Set the visual to show the operator's symbol 
 
      $("#operator").html("<h1>" + $(this).text() + "</h1>"); 
 
      operator = $(this).attr("value"); 
 
     } 
 

 
     // Checks if the equal button has been pressed. If so... 
 
     if ($(this).hasClass("equal")) { 
 

 
      // Lock the keyboard from being clicked 
 
      lockButtons = true;

+2

'!lockButtons'は' lockButtons'の値を変更しません – bejado

+0

var lockButtonsがまだfalseであり、true、lockButtonsがまだfalseであるかどうかをチェックするだけですか? –

+1

'if(!lockButtons)'は 'lockButtons'が' false'かどうかを調べます。大声で言ってください: "もしlockButtonsでなければ" – bejado

答えて

1
var lockButtons = false; 

この文はtrueまたはfalseの値を持つことができboolean変数を作成します。だから、lockbuttonif内のステートメントが実行されることはありませんfalseに設定されているのでif

if(lockbutton) 

に直接それを使用することができます。

if ($(this).hasClass("number") && !lockButtons) 

この文はあなたには明らかです。両方の条件が真であれば、if内のステートメントだけが実行されます。

また、声明が特定の場所に置かれている理由は、そのほとんどすべての上のコメントによって明らかになったと思います。

+0

は、deptの説明にもっと感謝します、私は何をしているのか理解しています!私はそれを使用していないのでおそらく私を捨てています。 –

+0

bejadoは上記のように「大騒ぎです」と言っています。ステートメントは何にも値を割り当てません。単なるテストです。 –

関連する問題