首页 > 解决方案 > 全局变量在三个连续的 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()
                }
    }
})();

标签: javascript

解决方案


第二个代码块是IIFE,因此它在加载时立即执行,并且具有私有范围。在那里声明的变量从外部是不可见的。

在哪里global声明?


推荐阅读