【问题标题】:I cant switch the boolean from true to false even though I have it written correctly即使我正确编写了布尔值,我也无法将布尔值从真切换为假
【发布时间】:2020-07-17 19:22:37
【问题描述】:

当我运行“switch”时,canChat 似乎并没有真正从 false 切换到 true,或者从 true 切换到 false,而它应该!我已经查过了,看来我是对的!

//deletes a  message sent by someone if canChat is false
let canChat = new Boolean(false);
    if(message.author.id.substring(0) === '475418097990500362' && !canChat){
        message.delete();
        message.channel.send('we\'re sorry, this bot it no longer available. Please stop using it.');
    }
    
    //Current Commands
    switch(args[0]){
        case 'switch' :
            canChat = !canChat; //This is what isn't working!
            if(canChat){
                message.reply('Switched to true');
            } else { message.reply('Switched to false');}
            break;```

【问题讨论】:

  • 布尔对象和布尔原语有很大不同。您应该将canChat 设置为纯false

标签: javascript boolean discord discord.js boolean-expression


【解决方案1】:

作为一个重点提到布尔值object/primitive 是两个不同的东西。

试试下面的编辑

let canChat = false;
if(message.author.id.substring(0) === '475418097990500362' && !canChat){
   ...
}

【讨论】:

    【解决方案2】:

    canChat 在您的示例中是一个对象:

    let canChat = new Boolean(false);
    if (canChat) {
        //it will be executed despite of false
    }
    

    你可以只使用真假原始值

    看看这个:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-21
      相关资源
      最近更新 更多