可以有人向我解释!lockButton在这个jquery中

时间:2017-03-03 06:07:59

标签: javascript jquery

这是一个简单的计算器练习的部分代码:

从我的理解中!反转布尔值,所以既然lockButtons设置为false,它现在在第一个if语句中是真的吗?但是稍后在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;

1 个答案:

答案 0 :(得分:1)

var lockButtons = false;

此语句创建一个boolean变量,其值可以为truefalse。因此,您可以直接在if

中使用它
if(lockbutton)

由于lockbutton设置为falseif内的语句将不会被执行。

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

此声明应该清楚。如果两个条件都为真,那么只会执行if内的语句。

此外,我认为很清楚为什么几乎所有这些评论都在某个地方发表声明。