正确的 for/in 变量声明

发布于 2024-10-27 14:50:16 字数 777 浏览 2 评论 0原文

for/in 循环中声明循环特定变量的正确语法是什么?

前两个似乎都可以工作(并且不会在 Google Closure 编译器中引发任何标志),但只有第三个通过了 Crockford 的 JS Lint。我不愿意使用它,主要是因为它不紧凑。

JSLint 抱怨 val 是一个坏变量(当我不添加 var 时),或者声明应该被移动。

第一个或第二个选项有什么缺点吗?我应该使用什么? (示例假设 str 是声明的字符串,vals 是声明的对象)

1. 无声明:

for(val in vals)
{
    if(vals.hasOwnProperty(val))
    {
        str += val;
    }
}

2. 在 'for' var 声明中:

for(var val in vals)
{
    if(vals.hasOwnProperty(val))
    {
        str += val;
    }
}

3. 在循环之外 var 声明:

var val;
for(val in vals)
{
    if(vals.hasOwnProperty(val))
    {
        str += val;
    }
}

What is the proper syntax for declaring the loop-specific variable in a for/in loop?

The first two both seem to work (and don't raise any flags in Google Closure Compiler), but only the third one passes Crockford's JS Lint. I am reluctant to use it, mostly because it is not compact.

JSLint complains that either val is a bad variable (when I don't add var), or that the declaration should be moved.

Are there any drawbacks to the first or second option? What should I be using? (Examples assume str is a declared string and vals is a declared object)

1. No declaration:

for(val in vals)
{
    if(vals.hasOwnProperty(val))
    {
        str += val;
    }
}

2. In 'for' var declaration:

for(var val in vals)
{
    if(vals.hasOwnProperty(val))
    {
        str += val;
    }
}

3. Outside the loop var declaration:

var val;
for(val in vals)
{
    if(vals.hasOwnProperty(val))
    {
        str += val;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哽咽笑 2024-11-03 14:50:16

请随意忽略 JSLint。这比什么都重要。第二个和第三个在功能上是相同的,随意使用其中一个(我使用第二个)。第一个暴露了全局“val”变量,所以不要这样做:)

更难发现/查找。

var a, b, c, d, e;

仅供参考,第三个背后的原因是内联变量声明比在函数顶部

Feel free to ignore JSLint. It's a guideline more than anything. The 2nd and 3rd are functionally identical, feel free to use either (I use the 2nd). The first exposes a global 'val' variable, so don't do it :)

FYI the reasoning behind the 3rd is that inline variable declarations are much harder to spot/find than:

var a, b, c, d, e;

at the top of a function.

七月上 2024-11-03 14:50:16

除了变量声明背后的 cwolves 推理之外,JSLint 还考虑 变量提升

话虽如此,在编写 for 循环时我更喜欢选项#2。不过,我确实在函数的顶部定义了其余的变量。所以我喜欢:

function foo(a) {
  var b, c = {d: "e", f: "g"};

  for (var i = 0, j = a.length; i < j; i += 1) {
    ...
  }

  for (var h in c) {
    if (c.hasOwnProperty(h)) {
      ...
    }
  }      
}

In addition to cwolves reasoning behind the variable declaration, JSLint is also considering variable hoisting.

That being said, I prefer option #2 when writing for loops. I do define the rest of my variables at the top of the function, though. So I like:

function foo(a) {
  var b, c = {d: "e", f: "g"};

  for (var i = 0, j = a.length; i < j; i += 1) {
    ...
  }

  for (var h in c) {
    if (c.hasOwnProperty(h)) {
      ...
    }
  }      
}
魂归处 2024-11-03 14:50:16

1. 选项

val 变量成为隐式全局变量。
隐式全局变量 100% 不好,应该避免。

2. 和 3. 选项

val 变量成为包含范围的变量。如果您的代码位于函数内部,则 val 变量将成为该函数的局部变量。两个选项是等效的。 (我更喜欢 2. 选项。)

JSLint 会抛出异常,因为 Crockford 的想法是始终在代码顶部声明所有变量。这对于较大的代码(例如 > 100 行)更有意义 - 在这种情况下,您可以考虑将声明移出 for 和 for-in 语句,并将它们放在代码的顶部。这样您就可以很好地了解所有局部变量。

1. Option

The val variable becomes an implicit global variable.
Implicit global variables are 100% bad and should be avoided.

2. and 3. Option

The val variable becomes a variable of the containing scope. If your code is inside a function, then the val variable becomes a local variable of that function. Both options are equivalent. (I prefer the 2. option.)

JSLint will throw because Crockford's idea is to always declare all variables at the top of the code. This makes more sense for code that is larger in size (like > 100 lines) - in that case you may consider to move the declarations out of the for and for-in statements, and put them at the top of the code. That way you will have a good overview of all the local variables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文