Search code examples
javascriptprototypejsopera

How to exclude added to Array methods from processing in "for..in" loop? (javascript)


I've added some useful helpers to Array (such as toSource() for Opera). And now for..in returns the functions with normal properties.

I'm using for..in now, because the code is easier to read with it. And it's a native functional of js, so must be faster.

But adding type checks in loop makes it easier to use classic for(;;).

Is there any methods to avoid for..in enumerate functions ?

Cross-browser work is not very necessary (must work in Opera), but speed is important.

Thank you.


Edit:
Is there any ability to avoid for..in enumerate functions or custom properties from any Object ?


Solution

  • You should never use for..in loops to iterate over array elements. for..in is designed for iterating over properties, and should only be used for that, for exactly the reason you've just described. Many libraries modify array, date, etc prototypes, so you should not rely on for..in iterating just the array elements. Use the for(;;) method, it's guaranteed to do what you want. And it's no faster than a for..in loop, because it's native to javascript as well.

    For more info, read about it in the prototype.js library.