Javascript 初学者问题:Bool 还是 Int?
Javascript beginner question: Bool or Int?
我是 Javascript 初学者,我想问一下为什么我的代码中会出现这样的问题
var whoisplaying = 2;
function laX(lid) {
if (whoisplaying == 2) {
document.getElementById(lid).textContent = "X";
minus();
} else {
prompt("hi");
plus();
}
}
function minus() {
whoisplaying = 1;
}
function plus() {
whoisplaying = 2;
}
在上面的这个上,我对应用于几个按钮的点击功能没有任何问题,而如果我将 var whosiplaying 设置为 bool,就像我将在下一个片段中显示的那样,它不起作用:else 语句永远不会工作,var 永远是真的。
var whoisplaying = true;
function laX(lid) {
if (whoisplaying == true) {
document.getElementById(lid).textContent = "X";
minus();
} else {
prompt("hi");
plus();
}
}
function minus() {
whoisplaying = false;
}
function plus() {
whoisplaying = true;
}
谁能解释一下原因,这样我以后就不会再犯错误了?谢谢。
这是您的两个示例,它们的行为方式相同!请注意,当您检查布尔值
时,if(whoisplaying == true)
可能只是 if(whoisplaying)
使用数字
var whoisplaying = 2;
function laX() {
if(whoisplaying == 2) {
document.getElementById("lid").textContent="X";
minus();
}else {
prompt("hi");
plus();
}
}
function minus() {
whoisplaying = 1;
}
function plus() {
whoisplaying = 2;
}
<p id="lid"></p>
<button onclick="laX()">Click me</button>
使用布尔值
var whoisplaying = true;
function laX() {
if(whoisplaying == true) {
document.getElementById("lid").textContent="X";
minus();
}else {
prompt("hi");
plus();
}
}
function minus() {
whoisplaying = false;
}
function plus() {
whoisplaying = true;
}
<p id="lid"></p>
<button onclick="laX()">Click me</button>
好的,因为您添加了一条评论
Don't ask me why but using boolenas it wasn't working when I tried it before... the else statement wasn't working...now it works both way.
我意识到这个错误是你代码中的错字,我认为你在你的 if 语句中输入了 =
而不是 ==
尽管给定的代码没有那个错字,所以这使得 if 语句每次都将 true
值分配给该变量并且该表达式的返回值是真实的,因此 else 永远不会执行,但总是执行 if 语句的主体,这是一个示例
<p id="lid"></p>
<button onclick="laX()">Click me</button>
使用布尔值
var whoisplaying = true;
function laX() {
if(whoisplaying = true) {
document.getElementById("lid").textContent="X";
minus();
}else {
prompt("hi");
plus();
}
}
function minus() {
whoisplaying = false;
}
function plus() {
whoisplaying = true;
}
<p id="lid"></p>
<button onclick="laX()">Click me</button>
我是 Javascript 初学者,我想问一下为什么我的代码中会出现这样的问题
var whoisplaying = 2;
function laX(lid) {
if (whoisplaying == 2) {
document.getElementById(lid).textContent = "X";
minus();
} else {
prompt("hi");
plus();
}
}
function minus() {
whoisplaying = 1;
}
function plus() {
whoisplaying = 2;
}
在上面的这个上,我对应用于几个按钮的点击功能没有任何问题,而如果我将 var whosiplaying 设置为 bool,就像我将在下一个片段中显示的那样,它不起作用:else 语句永远不会工作,var 永远是真的。
var whoisplaying = true;
function laX(lid) {
if (whoisplaying == true) {
document.getElementById(lid).textContent = "X";
minus();
} else {
prompt("hi");
plus();
}
}
function minus() {
whoisplaying = false;
}
function plus() {
whoisplaying = true;
}
谁能解释一下原因,这样我以后就不会再犯错误了?谢谢。
这是您的两个示例,它们的行为方式相同!请注意,当您检查布尔值
时,if(whoisplaying == true)
可能只是 if(whoisplaying)
使用数字
var whoisplaying = 2;
function laX() {
if(whoisplaying == 2) {
document.getElementById("lid").textContent="X";
minus();
}else {
prompt("hi");
plus();
}
}
function minus() {
whoisplaying = 1;
}
function plus() {
whoisplaying = 2;
}
<p id="lid"></p>
<button onclick="laX()">Click me</button>
使用布尔值
var whoisplaying = true;
function laX() {
if(whoisplaying == true) {
document.getElementById("lid").textContent="X";
minus();
}else {
prompt("hi");
plus();
}
}
function minus() {
whoisplaying = false;
}
function plus() {
whoisplaying = true;
}
<p id="lid"></p>
<button onclick="laX()">Click me</button>
好的,因为您添加了一条评论
Don't ask me why but using boolenas it wasn't working when I tried it before... the else statement wasn't working...now it works both way.
我意识到这个错误是你代码中的错字,我认为你在你的 if 语句中输入了 =
而不是 ==
尽管给定的代码没有那个错字,所以这使得 if 语句每次都将 true
值分配给该变量并且该表达式的返回值是真实的,因此 else 永远不会执行,但总是执行 if 语句的主体,这是一个示例
<p id="lid"></p>
<button onclick="laX()">Click me</button>
使用布尔值
var whoisplaying = true;
function laX() {
if(whoisplaying = true) {
document.getElementById("lid").textContent="X";
minus();
}else {
prompt("hi");
plus();
}
}
function minus() {
whoisplaying = false;
}
function plus() {
whoisplaying = true;
}
<p id="lid"></p>
<button onclick="laX()">Click me</button>