Why does local variable kill my global variable?

时间:2022-04-01 16:46:24

Sorry for this question, but this issue really screwed up my day.

对不起这个问题,但这个问题真的搞砸了我的一天。

The following Code alerts 10 as it should:

以下代码警告10应该:

var globalId='10';  
function check(){  
    alert(globalId);  
}  
check();

But this next code alerts undefined:

但是下一个代码警告未定义:

var globalId='10';  
function check(){  
    alert(globalId); 
    var globalId; 
}  
check();

I am aware that if I declare a variable in a function its a local variable, but if I already declared it as global, how can it be that my alerts says undefined?

我知道如果我在函数中声明一个变量,它是一个局部变量,但如果我已经将它声明为全局变量,那么我的警报怎么说未定义呢?

This is an easy example, but in my original code I did a lot of stuff in between the beginning of the function, then a long way down I checked to see if globalId was defined, else define it: if(!globalId){var globalId;} This meant that my alert situated at the top of the function generated undefined, as if JavaScript first executed the whole function, just to see if any variables 'might' be declared, and if so, declare them and therefore my alert pointed to an 'undeclared' variable.

这是一个简单的例子,但在我的原始代码中,我在函数的开头之间做了很多东西,然后我检查了很长时间以查看是否定义了globalId,否则定义它:if(!globalId){var globalId;}这意味着位于函数顶部的警报生成未定义,就像JavaScript首先执行整个函数一样,只是为了查看是否可以声明任何变量,如果是,则声明它们,因此我的警报指向到'未声明'的变量。

Can anybody explain to me why this happen, and if it is true that JavaScript "pre-declares" all variables before executing a function, even variables declared in conditions not even met?

任何人都可以向我解释为什么会发生这种情况,并且如果JavaScript在执行函数之前“预先声明”了所有变量,那么甚至在甚至不满足的条件下声明的变量也是如此?

7 个解决方案

#1


23  

In javascript, you should know there is something called as HOISTING.

在javascript中,你应该知道有一些叫做HOISTING的东西。

What this essentially means is, when you declare any local variables, the variable declaration is automatically carried to top of the scope.

这实际上意味着,当您声明任何局部变量时,变量声明会自动传递到范围的顶部。

eg:-

var globalId='10';
function check(){
alert(globalId); var globalId; }
check(); 

Changes to -

变更为 -

var globalId='10';
function check(){
var globalId;
alert(globalId);}
check(); 

Since globalID is still not assigned any value, it returns undefined in your output. The local variables always get priority over the global variables with same name.

由于globalID仍未分配任何值,因此它会在输出中返回undefined。局部变量总是优先于具有相同名称的全局变量。

#2


16  

Yes, all variables declared anywhere in a function are local to that function and exist throughout the function's code; they will be used in preference to globals of the same name.

是的,函数中任何地方声明的所有变量都是该函数的本地变量,并且存在于整个函数的代码中;它们将优先用于同名的全局变量。

From https://developer.mozilla.org/en/JavaScript/Guide/Values,_Variables,_and_Literals#Variable_Scope :

来自https://developer.mozilla.org/en/JavaScript/Guide/Values,_Variables,_and_Literals#Variable_Scope:

JavaScript does not have block statement scope; rather, it will be local to the code that the block resides within. [...] Another unusual thing about variables in JavaScript is that you can refer to a variable declared later, without getting an exception. This concept is known as hoisting; variables in JavaScript are in a sense "hoisted" or lifted to the top of the function or statement.

JavaScript没有块语句范围;相反,它将是块所在代码的本地代码。 [...] JavaScript中变量的另一个不同寻常之处在于,您可以引用稍后声明的变量,而不会出现异常。这个概念被称为吊装; JavaScript中的变量在某种意义上是“提升”或提升到函数或语句的顶部。

#3


12  

In your second portion of code, the local variable masks the global one.

在代码的第二部分中,局部变量掩盖了全局变量。

var globalId='10';

function check() {
    // Your are defining a local variable in this function
    // so, the global one is not visible.
    alert(globalId);
    var globalId;
}

check(); 


The fact that yopur var statement is at the end of the function's definition doesn't change anything : the global variable is masked for the whole function.

yopur var语句位于函数定义末尾的事实不会改变任何内容:全局变量被屏蔽为整个函数。

So, for the whole execution of the function, the globalId variable will reference the local one, and not the global one.

因此,对于函数的整个执行,globalId变量将引用本地变量,而不是全局变量。

Outside of that function, though, the global variable will still exist -- it will just not be seen from inside the function, because of the var statement.

但是,在该函数之外,全局变量仍然存在 - 由于var语句,它不会从函数内部看到。

#4


3  

As has been said, conforming to JavaScript scoping rules, the local variable masks the global for the entire function. However the global variable may be accessd, try the following

如前所述,符合JavaScript作用域规则,局部变量会屏蔽整个函数的全局变量。但是,可以访问全局变量,请尝试以下操作

var globalId='10';

function check() {
    // Your are defining a local variable in this function
    // so, the global one is not visible.
    alert('Local : ' + globalId + ', Global : ' + window.globalId);
    var globalId;
}

check(); 

#5


0  

you declare NEW variable globalId inside function scope, so its undefined and this is correct. And no, it's not kill your global variable, you can check it by adding alert(globalId); after check(); call.

你在函数范围内声明了新变量globalId,所以它是未定义的,这是正确的。不,它不是杀死你的全局变量,你可以通过添加alert(globalId)来检查它;检查后();呼叫。

#6


0  

as if Javascript first executed the whole function, just to see if any variables 'might' be declared

就像Javascript首先执行整个函数一样,只是为了查看是否可以声明任何变量

this is the answer, more or less. The JavaScript interpreter looks for variable declarations within each scope, then "moves them" to the top of the scope.

这或多或少都是答案。 JavaScript解释器在每个范围内查找变量声明,然后将它们“移动”到范围的顶部。

#7


0  

var globalId='10';  
    function check(){ 
        let globalId = '5'; 
        alert(globalId); 

    }  
    check();

// use let to set a local variable between any {}

#1


23  

In javascript, you should know there is something called as HOISTING.

在javascript中,你应该知道有一些叫做HOISTING的东西。

What this essentially means is, when you declare any local variables, the variable declaration is automatically carried to top of the scope.

这实际上意味着,当您声明任何局部变量时,变量声明会自动传递到范围的顶部。

eg:-

var globalId='10';
function check(){
alert(globalId); var globalId; }
check(); 

Changes to -

变更为 -

var globalId='10';
function check(){
var globalId;
alert(globalId);}
check(); 

Since globalID is still not assigned any value, it returns undefined in your output. The local variables always get priority over the global variables with same name.

由于globalID仍未分配任何值,因此它会在输出中返回undefined。局部变量总是优先于具有相同名称的全局变量。

#2


16  

Yes, all variables declared anywhere in a function are local to that function and exist throughout the function's code; they will be used in preference to globals of the same name.

是的,函数中任何地方声明的所有变量都是该函数的本地变量,并且存在于整个函数的代码中;它们将优先用于同名的全局变量。

From https://developer.mozilla.org/en/JavaScript/Guide/Values,_Variables,_and_Literals#Variable_Scope :

来自https://developer.mozilla.org/en/JavaScript/Guide/Values,_Variables,_and_Literals#Variable_Scope:

JavaScript does not have block statement scope; rather, it will be local to the code that the block resides within. [...] Another unusual thing about variables in JavaScript is that you can refer to a variable declared later, without getting an exception. This concept is known as hoisting; variables in JavaScript are in a sense "hoisted" or lifted to the top of the function or statement.

JavaScript没有块语句范围;相反,它将是块所在代码的本地代码。 [...] JavaScript中变量的另一个不同寻常之处在于,您可以引用稍后声明的变量,而不会出现异常。这个概念被称为吊装; JavaScript中的变量在某种意义上是“提升”或提升到函数或语句的顶部。

#3


12  

In your second portion of code, the local variable masks the global one.

在代码的第二部分中,局部变量掩盖了全局变量。

var globalId='10';

function check() {
    // Your are defining a local variable in this function
    // so, the global one is not visible.
    alert(globalId);
    var globalId;
}

check(); 


The fact that yopur var statement is at the end of the function's definition doesn't change anything : the global variable is masked for the whole function.

yopur var语句位于函数定义末尾的事实不会改变任何内容:全局变量被屏蔽为整个函数。

So, for the whole execution of the function, the globalId variable will reference the local one, and not the global one.

因此,对于函数的整个执行,globalId变量将引用本地变量,而不是全局变量。

Outside of that function, though, the global variable will still exist -- it will just not be seen from inside the function, because of the var statement.

但是,在该函数之外,全局变量仍然存在 - 由于var语句,它不会从函数内部看到。

#4


3  

As has been said, conforming to JavaScript scoping rules, the local variable masks the global for the entire function. However the global variable may be accessd, try the following

如前所述,符合JavaScript作用域规则,局部变量会屏蔽整个函数的全局变量。但是,可以访问全局变量,请尝试以下操作

var globalId='10';

function check() {
    // Your are defining a local variable in this function
    // so, the global one is not visible.
    alert('Local : ' + globalId + ', Global : ' + window.globalId);
    var globalId;
}

check(); 

#5


0  

you declare NEW variable globalId inside function scope, so its undefined and this is correct. And no, it's not kill your global variable, you can check it by adding alert(globalId); after check(); call.

你在函数范围内声明了新变量globalId,所以它是未定义的,这是正确的。不,它不是杀死你的全局变量,你可以通过添加alert(globalId)来检查它;检查后();呼叫。

#6


0  

as if Javascript first executed the whole function, just to see if any variables 'might' be declared

就像Javascript首先执行整个函数一样,只是为了查看是否可以声明任何变量

this is the answer, more or less. The JavaScript interpreter looks for variable declarations within each scope, then "moves them" to the top of the scope.

这或多或少都是答案。 JavaScript解释器在每个范围内查找变量声明,然后将它们“移动”到范围的顶部。

#7


0  

var globalId='10';  
    function check(){ 
        let globalId = '5'; 
        alert(globalId); 

    }  
    check();

// use let to set a local variable between any {}