We all know that for-in-loops on arrays are absolutely evil. Still, they are often used and the caused errors are complicated to trace down, especially when happening browser-dependent for example because of indexOf
-shims or such.
So, I have coded this simple snippet which adds an enumerable getter for a "error
" property on Array.prototype
(not for use in production code):
Object.defineProperty(Array.prototype, "error", {
enumerable: true,
get: function() {
if (this === Array.prototype) // that looks OK
return undefined;
if (window.confirm("Somebody who coded the site you're viewing runs through an Array with a for-in-loop.\nShame on him!\n\nDo you want to raise an Error to trace the origin?"))
throw new SyntaxError("Array traverse with for-in-loop, touching Array.prototype's 'error' property :-)");
}
});
You can add it as a greasemonkey script for all domains, and you will see alerts on nearly every site :-) Most of them are caused by calls to jQuery.extend
with questionable arguments, btw.
My question is now: Are there any situations that legitimate such "wrong" loops, or anything else causing false-positive alerts?
I am wondering how this would affect the usefulness of my code.