0

This is a function in the Modernizr.js. http://modernizr.github.com/Modernizr/annotatedsource.html#section-21

Function test_props accepts props which is an array, then iterates it using a for-in loop. My question is: since props is an array, it has a length property. And normal for loop is faster than for-in loop. Why using for-in loop?

function test_props( props, callback ) {
        for ( var i in props ) {
            if ( m_style[ props[i] ] !== undefined && ( !callback || callback( props[i], modElem ) ) ) {
                return true;
            }
        }
    }
Chef
  • 633
  • 5
  • 17
  • 1
    If `props` is an array, they probably shouldn't be using `for..in`. If that's an object, `for..in` is fine. – John Dvorak Feb 23 '13 at 13:49
  • It is considered bad practice to use a for in loop in general and it should be avoided as much as possible. There are cases where it can’t be avoid for instance if you are iterating though a JSON object. –  Feb 23 '13 at 13:52
  • @NikhilAgrawal It's no use to iterate over JSON object, it's a global native JS object... – Teemu Feb 23 '13 at 14:05

0 Answers0