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

查看:32
本文介绍了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 在含义或性能上绝对没有区别.

There is absolutely no difference in meaning or performance, in JavaScript or ActionScript.

var 是解析器的指令,不是 在运行时执行的命令.如果在函数体 (*) 的任何位置已将特定标识符声明为 var 一次或多次,则块中对该标识符的所有使用都将引用局部变量.将 value 声明为循环内、循环外或两者的 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 并执行上面的 var i;),并且 jslint 会抱怨在你这里.但是 IMO 保留两个 var 更易于维护,将所有相关代码保存在一起,而不是在函数顶部添加额外的、容易忘记的代码.

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 疣(最好让变量默认为本地);我不认为我有责任在 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天全站免登陆