JavaScript变量声明在循环外部或内部? [英] JavaScript variables declare outside or inside loop?

查看:122
本文介绍了JavaScript变量声明在循环外部或内部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在AS3中,我认为你应该初始化循环外的所有变量以提高性能。这也是JavaScript的情况吗?哪个更好/更快/最佳实践?

In AS3 I believe you should initialise all variables outside loops for increased performance. Is this the case with JavaScript as well? Which is better / faster / best-practice?

var value = 0;

for (var i = 0; i < 100; i++)
{
    value = somearray[i];
}

for (var i = 0 ; i < 100; i++)
{
    var value = somearray[i];
}


推荐答案

绝对有在JavaScript或ActionScript中,在意义或性能方面没有区别

var 是指令解析器,在运行时执行的命令。如果在函数体(*)中的任何地方声明了一个或多个特定标识符 var ,那么块中该标识符的所有使用都将引用局部变量。是否在循环内,循环外或两者中声明为 var 没有区别。

var is a directive for the parser, and not a command executed at run-time. If a particular identifier has been declared var once or more anywhere in a function body(*), then all use of that identifier in the block will be referring to the local variable. It makes no difference whether value is declared to be var inside the loop, outside the loop, or both.

因此,你应该写出你认为最具可读性的。我不同意Crockford的说法,将所有变量放在函数顶部总是最好的。对于在代码段中临时使用变量的情况,最好在该部分中声明 var ,因此该部分可以单独使用并可以进行复制粘贴。否则,在重构期间将几行代码复制粘贴到新函数中,而无需单独挑选和移动关联的 var ,并且您自己也是偶然的全局。

Consequently you should write whichever you find most readable. I disagree with Crockford that putting all the vars at the top of a function is always the best thing. For the case where a variable is used temporarily in a section of code, it's better to declare var in that section, so the section stands alone and can be copy-pasted. Otherwise, copy-paste a few lines of code to a new function during refactoring, without separately picking out and moving the associated var, and you've got yourself an accidental global.

特别是:

for (var i; i<100; i++)
    do something;

for (var i; i<100; i++)
    do something else;

Crockford建议您删除第二个 var (或删除 var s并执行上面的 var i; ),jslint会为此向你发出警告。但IMO更容易维护 var s,将所有相关代码保存在一起,而不是在函数顶部有一个额外的,容易被遗忘的代码。

Crockford will recommend you remove the second var (or remove both vars and do var i; above), and jslint will whinge at you for this. But IMO it's more maintainable to keep both vars, keeping all the related code together, instead of having an extra, easily-forgotten bit of code at the top of the function.

我个人倾向于声明 var 在代码的独立部分中第一次赋值变量,无论是在同一个函数的某个其他部分中没有相同的变量名的另一个单独用法。对我来说,必须声明 var 根本就是一个不受欢迎的JS wart(将变量默认为local更好);我不认为我有责任在JavaScript中复制[旧版本] ANSI C的限制。

Personally I tend to declare as var the first assignment of a variable in an independent section of code, whether or not there's another separate usage of the same variable name in some other part of the same function. For me, having to declare var at all is an undesirable JS wart (it would have been better to have variables default to local); I don't see it as my duty to duplicate the limitations of [an old revision of] ANSI C in JavaScript as well.

(*:除了嵌套函数之外机构)

(*: other than in nested function bodies)

这篇关于JavaScript变量声明在循环外部或内部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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