开发者

Added function to Array in JavaScript, how to keep function out of for...in?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜