3 ms·
He could have run it through JSLint. It catches not wrapping the body of a for-in block in a hasOwnProperty conditional. Example: var arr = [0,1,2,3,4,5]; for
by warfangle 17y ago
He could have run it through JSLint. It catches not wrapping the body of a for-in block in a hasOwnProperty conditional.
Example:
var arr = [0,1,2,3,4,5];
for(var p in arr) { console.log(arr[p]); }
Array.prototype.foo = "bar";
for(var p in arr) {
if(arr.hasOwnProperty(p)) {
console.log(arr[p]);
}
}
for(var p in arr) { console.log(arr[p]); }
If you run this in firebug's console, you'll notice that the first two loops work as expected, even after changing the prototype of the Array object. The third one, however, will spit out "bar" as well.
JSLint can be mean to your code, but it helps catch an enormous amount of small bugs like this caused by JS's warts.