在JavaScript中,DRY原则是指"Don’t Repeat Yourself",即不要重复自己的代码。它是一种编程原则,强调避免在代码中出现重复的逻辑、功能和结构。
遵循DRY原则有以下几个好处:
● 例如我们之前猜游戏的代码出现了很多的重复代码,这节课我们就来重构它
let secretnumber = Math.trunc(Math.random() * 20) + 1;
let score = 20;
let highscore = 0;
document.querySelector('.check').addEventListener('click', function () {
const guess = Number(document.querySelector('.guess').value);
console.log(guess);
if (!guess) {
document.querySelector('.message').textContent = '⛔No Number';
} else if (guess === secretnumber) {
document.querySelector('.message').textContent = 'Correct Number';
document.querySelector('body').style.backgroundColor = '#60b347';
document.querySelector('.number').style.width = '30rem';
document.querySelector('.number').textContent = secretnumber;
if (highscore < score) {
highscore = score;
document.querySelector('.highscore').textContent = highscore;
}
} else if (guess > secretnumber) {
if (score > 1) {
document.querySelector('.message').textContent = 'Too high';
score--;
document.querySelector('.score').textContent = score;
} else {
document.querySelector('.message').textContent = 'You lost the game!';
document.querySelector('.score').textContent = 0;
}
} else if (guess < secretnumber) {
if (score > 1) {
document.querySelector('.message').textContent = 'Too low';
score--;
document.querySelector('.score').textContent = score;
} else {
document.querySelector('.message').textContent = 'You lost the game';
document.querySelector('.score').textContent = 0;
}
}
});
document.querySelector('.again').addEventListener('click', function () {
score = 20;
document.querySelector('.score').textContent = score;
document.querySelector('body').style.backgroundColor = '#222';
document.querySelector('.message').textContent = 'Start guessing...';
document.querySelector('.number').style.width = '15rem';
document.querySelector('.number').textContent = '?';
document.querySelector('.guess').value = '';
secretnumber = Math.trunc(Math.random() * 20) + 1;
});
这是重构之前的代码
let secretnumber = Math.trunc(Math.random() * 20) + 1;
let score = 20;
let highscore = 0;
document.querySelector('.check').addEventListener('click', function () {
const guess = Number(document.querySelector('.guess').value);
console.log(guess);
if (!guess) {
document.querySelector('.message').textContent = '⛔No Number';
} else if (guess === secretnumber) {
document.querySelector('.message').textContent = 'Correct Number';
document.querySelector('body').style.backgroundColor = '#60b347';
document.querySelector('.number').style.width = '30rem';
document.querySelector('.number').textContent = secretnumber;
if (highscore < score) {
highscore = score;
document.querySelector('.highscore').textContent = highscore;
}
} else if (guess !== secretnumber) {
if (score > 1) {
document.querySelector('.message').textContent =
guess > secretnumber ? 'Too high' : 'Too low';
score--;
document.querySelector('.score').textContent = score;
}
} else {
document.querySelector('.message').textContent = 'You lost the game!';
document.querySelector('.score').textContent = 0;
}
});
document.querySelector('.again').addEventListener('click', function () {
score = 20;
document.querySelector('.score').textContent = score;
document.querySelector('body').style.backgroundColor = '#222';
document.querySelector('.message').textContent = 'Start guessing...';
document.querySelector('.number').style.width = '15rem';
document.querySelector('.number').textContent = '?';
document.querySelector('.guess').value = '';
secretnumber = Math.trunc(Math.random() * 20) + 1;
});
这是重构的代码,将猜数字的太高和太低进行了优化,去除了很多重新的代码!
● 除此之外,我们还发现代码中有很多重复去修改message类的文本或者其他类的属性,我们可以将他存入一个函数中,然后将其直接调用即可
const displayMessage = function (message) {
document.querySelector('.message').textContent = message;
};
其他也一样,都通过函数的方式去重构建。这里不再重复!
我们通过重构精简了代码,并且使得代码的逻辑更加的清晰!!!