Monkeypatched code appearing in for...in loop over Array
<p>I have the following code in my web app's main JavaScript:</p>

<pre><code>    // uniq for arrays
if (!Array.prototype.getUnique) {
    Array.prototype.getUnique = function () {
        var u = {}, a = [];
        for (var i = 0, l = this.length; i &lt; l; ++i) {
            if (u.hasOwnProperty(this[i])) {
                continue;
            }
            a.push(this[i]);
            u[this[i]] = 1;
        }
        return a;
    }
}
</code></pre>

<p>It's a simple <code>uniq</code> clone for javascript, monkeypatched into the basic Array class. (Not here to debate monkeypatching, flame elsewhere please...)</p>

<p><code>getUnique()</code> works as intended, but now whenever I iterate over an Array with a <code>for...in</code> loop, an additional index called <code>getUnique</code> is passed to the iterator body, and when this false <code>n</code> is looked up, <code>getUnique</code>'s code is the value. (In other words: <code>for...in</code> is looping over the array as it should, but appending a <code>getUnique</code> as the last iteration)</p>

<p>What is going on here? There's another function monkeypatched in right above this one that works fine (<code>indexOf()</code>) and does not appear in the iterator. Here is some example code that triggers this issue:</p>

<pre><code>for (var r in result) {
    //tags = tags + result[r]["tags"].split(" ").join(", ")
    if (result[r]["tags"]) {
        var newtags = result[r]["tags"].split(" ");
        debug.log(newtags);
        for (var n in newtags) {
            debug.log("n is " + n);
            tags.push(newtags[n]);
        }
    }
}
</code></pre>

<p>The debug output from this snippet looks like:</p>

<pre><code>[14:22:26.090] [["camp", "furnitur", "wood"]]
[14:22:26.093] ["n is 0"]
[14:22:26.096] ["n is 1"]
[14:22:26.099] ["n is 2"]
[14:22:26.101] ["n is getUnique"]
</code></pre>

<p>What is going on here? Refactoring the monkeypatch and dropping it in a utility class is easy enough, but this is a really strange edge case to me. I have some ideas in my head about what is happening here, but those are just guesses. Can anyone explain?</p>
