如果多次声明相同的变量,是否会有任何问题? [英] Will I have any problems if I declare the same variable multiple times?

查看:89
本文介绍了如果多次声明相同的变量,是否会有任何问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以可以说我有一些代码:

So lets say I have some code:

//Javascript
var elements = [];
function addNumbah1(){
    var i = 1;
    elements.push(i);
}
function addNumbah2(){
    var i = 2;
    elements.push(i);
}

然后继续到addNumbah999(),每次声明 i 变量是一种不好的形式吗?那会破坏任何东西吗?我应该这样做:

And that goes on up to addNumbah999(), is it bad form to declare the i variable every time? Will that break anything? Should I do:

//Javascript
var elements = [];
var i
function addNumbah1(){
    i = 1;
    elements.push(i);
}
function addNumbah2(){
    i = 2;
    elements.push(i);
}

推荐答案

简短答案:,无论您声明了多少次,JS都会将所有变量声明提升到作用域的顶部他们:

Short answer: NO, JS hoists all variable declarations to the top of the scope, regardless of how many times you've declared them:

var i = 0
for (var i=0;i<10;i++)
{
    var j = i%2;//declared 10 times, on each iteration
}

将被翻译为

var i, j; //i is undefined at this point in the code.
for (i = 0;i<10;i++)
{
    j = i%2;//declared 10 times, on each iteration
}

在第一个示例中,您将 i 声明为函数范围内的变量,这是您必须进行的操作,以避免混乱全局范围.这些变量使用的内存在调用函数时分配,并在函数返回时释放(大致上, closures 构成异常,但这会使我们走得更远).考虑一下:

In your first example, you're declaring i as a variable in a function's scope, which is what you must do to avoid cluttering the global scope. The memory these variables use is allocated when the function is called, and deallocated when the function returns (roughly, closures form an exception, but that would take us to far). Consider this:

var i = 10;
function someF()
{
    var i = 1;
    alert(i);
}
someF();//alerts 1 <-- value of i, local to someF
alert(i);//10, global i is unchanged

但是如果您要省略 var :

function someF()
{
    i = 1;
    alert(i);
}

您会看到1被两次提醒.如果JS在当前作用域中找不到变量声明,它将在更高的作用域中查找,直到找到var.如果未找到任何变量,则JS会在最大范围(全局)中为您创建一个.在此处查看我的答案有关隐含全局变量如何工作的详细示例,或者阅读MDN页面,尤其是关于名称冲突的部分

You'll see that 1 is alerted twice. If JS can't find a variable declaration in the current scope, it will look in the higher scopes until a var is found. If no variable is found, JS will create one for you in the highest scope (global). Check my answer here on how implied globals work for a more detailed example, or read the MDN pages, especially the section on Name conflicts

最后,我想补充一下,全局变量,尤其是隐含的全局变量,是 evil .还知道ECMA6标准显然正在远离全局变量,并引入了对真正的块范围的支持.如您所见
哦,如果要检查函数是否使用隐式全局变量:'use strict'; 是一件好事:

Lastly, I'd like to add that globals, especially implied globals, are evil. Also know that the ECMA6 standard is clearly moving away from global variables and introduces support for true block-scopes. As you can see here
Oh, and if you want to check if a function uses implied globals: 'use strict'; is a great thing:

(function()
{
    'use strict';
    var localVar = 123;//ok
    impliedGlobal = 123;//TypeError!
}());

如您所见,不允许使用隐式全局变量.有关完整说明,请参见严格模式下的MDN

As you can see, implied globals are not allowed. See MDN on strict mode for the full explanation

这篇关于如果多次声明相同的变量,是否会有任何问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆