javascript - 全局变量在三个连续的 switch 函数中有效,但在第 4 个中无效(未定义)
问题描述
我有 4 个 switch 语句。我在所有这些中都使用相同的全局变量。前 3 个有效,但第 4 个无效。第 4 个写在前三个的正下方,所以这不是问题或范围之类的问题。它甚至不会提醒该值(控制台日志会说未定义该变量)。
有谁知道为什么会这样?我可以发布代码,但是代码真的很多,我不想让你淹没在无用的信息中。
谢谢你,D。
全局工作的部分(直接在不起作用的代码上方)。
function emShowError() {
switch (global){
case "sl":
if(email.validity.valueMissing) {
emailError.textContent = 'Prosim vnesite svoj elektronski naslov.';
} else if(email.validity.typeMismatch) {
emailError.textContent = 'Prosim vnesite veljaven elektronski naslov.';
}
emailError.className = 'error active';
break;
case "en":
if(email.validity.valueMissing) {
emailError.textContent = 'You need to enter an e-mail address EN.';
} else if(email.validity.typeMismatch) {
emailError.textContent = 'Entered value needs to be an e-mail address EN.';
}
emailError.className = 'error active';
break;
case "de":
if(email.validity.valueMissing) {
emailError.textContent = 'You need to enter an e-mail address DE.';
} else if(email.validity.typeMismatch) {
emailError.textContent = 'Entered value needs to be an e-mail address DE.';
}
emailError.className = 'error active';
break;
case "it":
if(email.validity.valueMissing) {
emailError.textContent = 'You need to enter an e-mail address IT.';
} else if(email.validity.typeMismatch) {
emailError.textContent = 'Entered value needs to be an e-mail address IT.';
}
emailError.className = 'error active';
break;
case "hr":
if(email.validity.valueMissing) {
emailError.textContent = 'You need to enter an e-mail address HR.';
} else if(email.validity.typeMismatch) {
emailError.textContent = 'Entered value needs to be an e-mail address HR.';
}
emailError.className = 'error active';
break;
case "ru":
if(email.validity.valueMissing) {
emailError.textContent = 'You need to enter an e-mail address RU.';
} else if(email.validity.typeMismatch) {
emailError.textContent = 'Entered value needs to be an e-mail address RU.';
}
emailError.className = 'error active';
break;
default:
if(email.validity.valueMissing) {
emailError.textContent = 'You need to enter an e-mail address.';
} else if(email.validity.typeMismatch) {
emailError.textContent = 'Entered value needs to be an e-mail address.';
}
emailError.className = 'error active';
}
};
未定义全局变量(称为全局)的部分。
alert(global);
(function(){
switch (global){
case "sl":
confirm('SI-Do you accept our private policy which can be found <a href="#">here</a>?')
if(false){
window.close()
}
case "en":
confirm('EN-Do you accept our private policy which can be found <a href="#">here</a>?')
if(false){
window.close()
}
case "de":
confirm('DE-Do you accept our private policy which can be found <a href="#">here</a>?')
if(false){
window.close()
}
case "it":
confirm('IT-Do you accept our private policy which can be found <a href="#">here</a>?')
if(false){
window.close()
}
case "hr":
confirm('HR-Do you accept our private policy which can be found <a href="#">here</a>?')
if(false){
window.close()
}
case "ru":
confirm('RU-Do you accept our private policy which can be found <a href="#">here</a>?')
if(false){
window.close()
}
}
})();
解决方案
第二个代码块是IIFE,因此它在加载时立即执行,并且具有私有范围。在那里声明的变量从外部是不可见的。
在哪里global
声明?
推荐阅读
- php - Laravel - 服务容器中单例和实例绑定之间的区别
- node-red - 带有 TTS 响应的出站呼叫,包括 DTMF 输入
- amazon-web-services - 机械土耳其人:外部问题无法从请求者界面工作
- git - 是否可以防止 git repo 使用全局 gitconfig 文件?
- java - android.content.res.Resources$NotFoundException:字符串资源 ID #0x0 问题与 Snackbar
- php - 使用 phpmailer 时捕获和显示错误的问题
- c# - 不允许抽象类的孙子覆盖其父 c#
- mysql - 根据多个“介于”标准选择结果
- javascript - 在页面重新加载时,只要选择它就显示表单
- php - 为什么我的文件上传功能无法正常工作?PHP