在这种情况下使用开关是否明智?因为在检查代码后,我得到注释以使用开关。另一个问题是,在这里我应该改变什么来重构方法的这一部分?
if(this.isGameOver) return;
if(square.classList.contains('checked') || square.classList.contains('flag')) return
if(square.classList.contains('bomb')) {
this.gameOver();
} else {
let total = square.getAttribute('data');
if(total != 0) {
square.classList.add('checked');
if(total == 1) square.classList.add('one');
if(total == 2) square.classList.add('two');
if(total == 3) square.classList.add('three');
if(total == 4) square.classList.add('four');
square.innerHTML = total;
return
}
this.checkSquare(currentId);
}
square.classList.add('checked');
答案 0 :(得分:3)
我会使用一个数组:
const total_classes = ["one", "two", "three", "four"];
if (total > 0 && total <= total_classes.length) {
square.classList.add(total_classes[total-1]);
}
答案 1 :(得分:0)
应该是这样吗?
const totalClasses = ['one', 'two', 'three', 'four'];
if(total > 0 && total <= totalClasses.length) {
square.classList.add('checked');
square.classList.add(totalClasses[total-1])
square.innerHTML = total;
return
}
答案 2 :(得分:0)
当说使用开关时,它是在说您的5个条件语句是否使用合计。
虽然Barmar建议使用数组是可取的,但重构工具建议您打开total。
例如:
switch(total){
case 1:
square.classList.add("one");
break;
case 2:
square.classList.add("two");
break;
case 3:
square.classList.add("three");
break;
case 4:
square.classList.add("four");
break;
}
编辑-我实际上将使用以下方法。读取imo更容易。
var classArr = square.classList;
if (classArr.contains('checked') || classArr.contains('flag')) {
return;
} else if (classArr.contains('bomb')) {
this.gameOver();
return;
} else {
var total = square.getAttribute('data');
var translate = { 0: "Zero", 1: "one", 2: "two", 3: "three", 4: "four" };
classArr.add('checked');
classArr.add(translate[total]);
square.innerHTML = total;
this.checkSquare(currentId);
}
答案 3 :(得分:0)
一些想法如何重构?它是扫雷器中用来检查单击的正方形周围的每个正方形的递归。
checkSquare(currentId) {
const isLeftEdge = (currentId % this.width === 0);
const isRightEdge = (currentId % this.width === this.width - 1);
setTimeout(() => {
if(currentId > 0 && !isLeftEdge) {
const newId = this.cells[parseInt(currentId) - 1].id;
const newSquare = document.getElementById(newId);
this.clicked(newSquare)
}
if(currentId > 9 && !isRightEdge) {
const newId = this.cells[parseInt(currentId) + 1 - this.width].id;
const newSquare = document.getElementById(newId);
this.clicked(newSquare);
}
if(currentId > 9) {
const newId = this.cells[parseInt(currentId) - this.width].id;
const newSquare = document.getElementById(newId);
this.clicked(newSquare);
}
if(currentId > 11 && !isLeftEdge) {
const newId = this.cells[parseInt(currentId) - 1 - this.width].id;
const newSquare = document.getElementById(newId);
this.clicked(newSquare);
}
if(currentId < 99 && !isRightEdge) {
const newId = this.cells[parseInt(currentId) + 1].id;
const newSquare = document.getElementById(newId);
this.clicked(newSquare);
}
if(currentId < 90 && !isLeftEdge) {
const newId = this.cells[parseInt(currentId) - 1 + this.width].id;
const newSquare = document.getElementById(newId);
this.clicked(newSquare);
}
if(currentId < 88 && !isRightEdge) {
const newId = this.cells[parseInt(currentId) + 1 + this.width].id;
const newSquare = document.getElementById(newId);
this.clicked(newSquare);
}
if(currentId < 90) {
const newId = this.cells[parseInt(currentId) + this.width].id;
const newSquare = document.getElementById(newId);
this.clicked(newSquare);
}
}, 10)
},