【发布时间】:2022-01-08 02:34:55
【问题描述】:
所以我知道嵌套代码很快就会变得丑陋。因此,我查找了一种防止嵌套的方法,发现您可以执行以下操作
if (user == null) return;
console.log('Deleting');
user.delete();
而不是像那样使用大括号
if (user != null) {
console.log('Deleting');
user.delete();
}
但是,在使用 if-else 语句时,我真的看不出这有什么帮助。例如:我的项目中有这段代码,我真的很想用比这更干净的方式编写。但我正在努力弄清楚如何做到这一点。
if (parseInt(year) == joinTime.getFullYear()) {
if (parseInt(month) == joinTime.getMonth() + 1) {
if (parseInt(day) == joinTime.getDay()) {
channel.send(message);
} else {
comparison(day, hour, minute);
}
} else {
comparison(day, hour, minute);
}
} else {
comparison(day, hour, minute);
}
【问题讨论】:
-
if (a && b && c) ... else ...…?!
标签: javascript if-statement nested conditional-statements coding-style