简体   繁体   English

有人可以向我解释一下!

[英]can someone explain to me !lockButton in this jquery

This is partial code from a simple calculator exercise: 这是一个简单的计算器练习中的部分代码:

From my understanding the ! 据我了解! reverses the boolean value, so since lockButtons was set to false, is it now true in the first if statement? 反转布尔值,因此由于lockButtons设置为false,所以在第一个if语句中它现在是否为true? But later below in the if check =, we set lockButtons to true so it stops any numbers from being inputted, it's probably simple but i can't wrap my head around it. 但是,在下面的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; 

var lockButtons = false;

This statement creates a boolean variable which which can have a value of true or false . 该语句创建一个boolean变量,其值可以为truefalse So you can use it directly in if 所以,你可以直接使用它if

if(lockbutton)

Since lockbutton is set to false the statements inside if will not be executed. 由于lockbutton设置为false因此if其中的语句将不执行。

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

This statement should be clear to you. 这句话对您应该很清楚。 if both the conditions are true then only the statements inside if will be executed. 如果两个条件都成立,则仅执行if的语句。

Also I think it's pretty clear why a statement has been put at a particular place by the comments above almost all of them. 我也很清楚,为什么几乎所有评论上面的评论都会在特定位置发表声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM