Remove hasOwnProperty checks in for-loops
authorTimo Tijhof <krinklemail@gmail.com>
Fri, 6 Jul 2018 02:46:56 +0000 (19:46 -0700)
committerTimo Tijhof <krinklemail@gmail.com>
Fri, 6 Jul 2018 02:46:56 +0000 (19:46 -0700)
There are some good uses of hasOwnProperty checks, but in for-loops
they only make sense if the object in question is not a plain
object (eg. class instance) and the code in question is explicitly
interested in instance properties. This is rare, but one example
exists in mediawiki.Uri, where it clones an instance by copying
properties from another instance.

Anywhere a plain object is looped over, the check is redundant
because Object.prototype has no enumerable own properties.

Both jQuery and ResourceLoader do not support environments
that alter Object.prototype to be that way, and the majority
of our for-loops actually already didn't have this check.

This commit removes the few hasOwnProperty checks that still
existed on for-loops over plain objects.

Side-note: jQuery.each() does an unfiltered for-loop, which means
           it too does not perform hasOwnProperty checks.

Change-Id: Ib5e5a04a5a8e21ab62b4e779117077ea5b0f8a47

resources/src/jquery.tablesorter/jquery.tablesorter.js
resources/src/mediawiki.api/upload.js
resources/src/mediawiki.debug/debug.js
resources/src/mediawiki.language/mediawiki.language.numbers.js
resources/src/mediawiki.page.watch.ajax.js

index 9417669..64d394c 100644 (file)
                if ( ts.collationTable ) {
                        // Build array of key names
                        for ( key in ts.collationTable ) {
-                               // Check hasOwn to be safe
-                               if ( Object.prototype.hasOwnProperty.call( ts.collationTable, key ) ) {
-                                       keys.push( mw.RegExp.escape( key ) );
-                               }
+                               keys.push( mw.RegExp.escape( key ) );
                        }
                        if ( keys.length ) {
                                ts.collationRegex = new RegExp( keys.join( '|' ), 'ig' );
index a6451ac..f343321 100644 (file)
@@ -40,9 +40,7 @@
        function getFirstKey( obj ) {
                var key;
                for ( key in obj ) {
-                       if ( Object.prototype.hasOwnProperty.call( obj, key ) ) {
-                               return key;
-                       }
+                       return key;
                }
        }
 
index dfb6967..37c0fac 100644 (file)
                        };
 
                        for ( id in panes ) {
-                               if ( !Object.prototype.hasOwnProperty.call( panes, id ) ) {
-                                       continue;
-                               }
-
                                $( '<div>' )
                                        .prop( {
                                                className: 'mw-debug-pane',
                                        .appendTo( $table );
 
                                for ( key in data ) {
-                                       if ( !Object.prototype.hasOwnProperty.call( data, key ) ) {
-                                               continue;
-                                       }
-
                                        $( '<tr>' )
                                                .append( $( '<th>' ).text( key ) )
                                                .append( $( '<td>' ).text( data[ key ] ) )
index 4360adc..f4194d3 100644 (file)
                for ( i = 0; i < arguments.length; i++ ) {
                        table = arguments[ i ];
                        for ( key in table ) {
-                               if ( Object.prototype.hasOwnProperty.call( table, key ) ) {
-                                       // The thousand separator should be deleted
-                                       flipped[ table[ key ] ] = key === ',' ? '' : key;
-                               }
+                               // The thousand separator should be deleted
+                               flipped[ table[ key ] ] = key === ',' ? '' : key;
                        }
                }
 
index 097653d..2002b9f 100644 (file)
 
                actionPaths = mw.config.get( 'wgActionPaths' );
                for ( key in actionPaths ) {
-                       if ( Object.prototype.hasOwnProperty.call( actionPaths, key ) ) {
-                               parts = actionPaths[ key ].split( '$1' );
-                               parts = parts.map( mw.RegExp.escape );
-                               m = new RegExp( parts.join( '(.+)' ) ).exec( url );
-                               if ( m && m[ 1 ] ) {
-                                       return key;
-                               }
-
+                       parts = actionPaths[ key ].split( '$1' );
+                       parts = parts.map( mw.RegExp.escape );
+                       m = new RegExp( parts.join( '(.+)' ) ).exec( url );
+                       if ( m && m[ 1 ] ) {
+                               return key;
                        }
                }