discord.js 中 if 语句之外的变量
Variables outside of an if statement in discord.js
我需要使用某些变量只有在触发时才有值,但我需要在 if
语句之外使用这些变量。有人知道怎么做吗?
代码:
let commandRan = false
let User = msg.author.id
if (msg.content.toLowerCase().startsWith("pe!command")){
msg.channel.send('Hello! say 1 or 2');
commandRan = true
User = msg.author.id
};
if (msg.content.toLowerCase().startsWith("1")){
if (commandRan === true) {
if (User === msg.author.id){
msg.channel.send("1!")
}
}
} else if (msg.content.toLowerCase().startsWith("2")){
msg.channel.send("2!")
}
在 if 方法外部声明变量并影响它在内部的值:
let v1; //this is the var declaration.
//your code
if(<some condition>){
v1 = "something";
}
//more code
if (v1 !== undefined){
console.log(v1);
//whatever you want to do in this case
}
注意undefined是保留关键字
可以在此处找到更多信息in the mozilla fondation docs
我需要使用某些变量只有在触发时才有值,但我需要在 if
语句之外使用这些变量。有人知道怎么做吗?
代码:
let commandRan = false
let User = msg.author.id
if (msg.content.toLowerCase().startsWith("pe!command")){
msg.channel.send('Hello! say 1 or 2');
commandRan = true
User = msg.author.id
};
if (msg.content.toLowerCase().startsWith("1")){
if (commandRan === true) {
if (User === msg.author.id){
msg.channel.send("1!")
}
}
} else if (msg.content.toLowerCase().startsWith("2")){
msg.channel.send("2!")
}
在 if 方法外部声明变量并影响它在内部的值:
let v1; //this is the var declaration.
//your code
if(<some condition>){
v1 = "something";
}
//more code
if (v1 !== undefined){
console.log(v1);
//whatever you want to do in this case
}
注意undefined是保留关键字
可以在此处找到更多信息in the mozilla fondation docs