在JavaScript中使用var重新定义局部变量 [英] Redefining a local variable with var in JavaScript
本文介绍了在JavaScript中使用var重新定义局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对JavaScript和局部变量有一个相当笼统的问题。我的问题是以下内容之间有什么区别,如果有的话:
I have a rather general question regarding JavaScript and local variables. My question is what is the difference between the following and if there is any:
function bla
{
var a = 2; // local variable
a = 3; // the local variable a gets a new value
// Would do the following line anything different
// (than simply asigning this value?)
var a = 4;
}
我想我不会得到两个名为a的局部变量。在其他语言中,这甚至是一个错误。那有什么用呢?
I suppose I won't get two local variables named a. In other languages this is even an error. So is there any use for this?
推荐答案
任何使用 var
在一个函数内被悬挂。对同一范围内的同一变量的后续使用无效。
Any use of var
within a function is hoisted. Subsequent uses on the same variable in the same scope have no effect.
它的含义与 a = 4;
完全相同。
这篇关于在JavaScript中使用var重新定义局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文