简体   繁体   English

Javascript for-in循环怪异行为

[英]Javascript for-in loop bizarre behavior

Suddenly, I am seeing a strange behavior. 突然,我看到一个奇怪的行为。 Here is a snippet: 这是一个片段:

// attributesList is an object with properties 0, 1, 2.
for (var j in attributesList) {
   // this loop should run 3 times.
   var attribute = attributesList[j];
}

As you can see from above, the loop should run 3 times. 从上面可以看到,循环应该运行3次。 But for some very strange reason it is being run 4 times. 但是由于某些非常奇怪的原因,它被运行了4次。 For the last iteration the value of j == "seek". 对于最后一次迭代,j ==“ seek”的值。

Even stranger, this same code base works on another git branch (no changes). 甚至更陌生,这个相同的代码库也可以在另一个git分支上运行(无更改)。 This only seems to happen when I run this from a particular git branch. 这似乎仅在我从特定git分支运行时才发生。

Is there any logical explanation for this mysterious "seek" property? 对于这种神秘的“寻求”属性,是否有任何逻辑解释? One thing I tried looking into is perhaps javascript version and any other versioning differences... but no luck here. 我尝试研究的一件事可能是javascript版本和其他版本差异...但是这里没有运气。

* Updated * * 更新 *

attributesList is an array of object type. attributesList是对象类型的数组。

The object in question has four enumerable properties. 有问题的对象具有四个可枚举的属性。 It probably inherits seek from its prototype object, which is why you think it only has three properties. 它可能从其原型对象继承了seek ,这就是为什么您认为它只有三个属性。

Your comment to MikeC adds weight to that: 您对MikeC的评论增加了重点:

when I evaluate the attributesList during run/debug time I do not see any property called "seek" 当我在运行/调试期间评估attributeList时,没有看到任何名为“ seek”的属性

As does your update: 和您的更新一样:

attributesList is an array of object type. attributesList是对象类型的数组。

That tells us that somewhere in your codebase (probably in a plugin), someone has been very naughty and added an enumerable property to Array.prototype . 这告诉我们,在您的代码库中的某个地方(可能是在插件中),有人很调皮,并向Array.prototype添加了一个可枚举的属性。 They can do that with various different syntaxes; 他们可以使用各种不同的语法来做到这一点。 if it's just the one property ( seek ), it's probably like this: 如果只是一个属性( seek ),则可能是这样的:

Array.prototype.seek = function() {
    // ...
};

That's poor practice, they should have made it non-enumerable: 那是很差的做法,他们应该使它无法枚举:

Object.defineProperty(Array.prototype, "seek", {
    value: function() {
        // ...
    }
});

But fundamentally, for-in is not for looping through arrays. 但是从根本上说, for-in并不是用于遍历数组。 See my other answer here for how to loop through arrays. 请参阅我的其他答案,以了解如何遍历数组。

Two excerpts from that long answer: 这个长答案的两个摘录:

Use forEach : 用于forEach

attributesList.forEach(function(attribute) {
   // ...
});

or if you really want to use for-in , add a hasOwnProperty check: 或者,如果您确实要使用for-in ,请添加hasOwnProperty检查:

for (var j in attributesList) {
   if (attributesList.hasOwnProperty(j)) {
      // this loop should run 3 times.
      var attribute = attributesList[j];
   }
}

or of course use a for loop. 或者当然使用for循环。 See the answer for details. 有关详细信息,请参见答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM