I extended Array to support indexOf in IE using this JavaScript function from 开发者_如何学PythonMozilla MDC.
Unfortunately, when using for...in syntax to iterate over the Array, the loop stops on indexOf instead of just numerical indexes.
Can I keep indexOf out of for...in syntax in Internet Explorer (it does in Chrome)? What makes the Array.length property and other Array functions so special that the for...in loop skips over them?
I know that switching to standard for syntax is a solution, but I would prefer a for...in fix.
Aside from avoiding the for...in notation for arrays, try to apply defensive programming:
When using the for (... in ...)
syntax in ES3 (current browsers), it's always recommended that you filter it:
var member;
for (member in someObject) {
if (someObject.hasOwnProperty(member)) {
someObject[member]; // do whatever you want with it
}
}
Other code could also enrich some object prototype.
精彩评论