From c22666dfaa4d5ff5a8eff4835c37597f4d88e786 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Thu, 5 Jul 2018 19:46:56 -0700 Subject: [PATCH] Remove hasOwnProperty checks in for-loops 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 --- .../src/jquery.tablesorter/jquery.tablesorter.js | 5 +---- resources/src/mediawiki.api/upload.js | 4 +--- resources/src/mediawiki.debug/debug.js | 8 -------- .../mediawiki.language.numbers.js | 6 ++---- resources/src/mediawiki.page.watch.ajax.js | 13 +++++-------- 5 files changed, 9 insertions(+), 27 deletions(-) diff --git a/resources/src/jquery.tablesorter/jquery.tablesorter.js b/resources/src/jquery.tablesorter/jquery.tablesorter.js index 9417669dde..64d394cd1f 100644 --- a/resources/src/jquery.tablesorter/jquery.tablesorter.js +++ b/resources/src/jquery.tablesorter/jquery.tablesorter.js @@ -723,10 +723,7 @@ 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' ); diff --git a/resources/src/mediawiki.api/upload.js b/resources/src/mediawiki.api/upload.js index a6451ac851..f343321e13 100644 --- a/resources/src/mediawiki.api/upload.js +++ b/resources/src/mediawiki.api/upload.js @@ -40,9 +40,7 @@ function getFirstKey( obj ) { var key; for ( key in obj ) { - if ( Object.prototype.hasOwnProperty.call( obj, key ) ) { - return key; - } + return key; } } diff --git a/resources/src/mediawiki.debug/debug.js b/resources/src/mediawiki.debug/debug.js index dfb6967958..37c0fac771 100644 --- a/resources/src/mediawiki.debug/debug.js +++ b/resources/src/mediawiki.debug/debug.js @@ -221,10 +221,6 @@ }; for ( id in panes ) { - if ( !Object.prototype.hasOwnProperty.call( panes, id ) ) { - continue; - } - $( '
' ) .prop( { className: 'mw-debug-pane', @@ -350,10 +346,6 @@ .appendTo( $table ); for ( key in data ) { - if ( !Object.prototype.hasOwnProperty.call( data, key ) ) { - continue; - } - $( '' ) .append( $( '' ).text( key ) ) .append( $( '' ).text( data[ key ] ) ) diff --git a/resources/src/mediawiki.language/mediawiki.language.numbers.js b/resources/src/mediawiki.language/mediawiki.language.numbers.js index 4360adc0cc..f4194d3b08 100644 --- a/resources/src/mediawiki.language/mediawiki.language.numbers.js +++ b/resources/src/mediawiki.language/mediawiki.language.numbers.js @@ -179,10 +179,8 @@ 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; } } diff --git a/resources/src/mediawiki.page.watch.ajax.js b/resources/src/mediawiki.page.watch.ajax.js index 097653d1cb..2002b9f87d 100644 --- a/resources/src/mediawiki.page.watch.ajax.js +++ b/resources/src/mediawiki.page.watch.ajax.js @@ -90,14 +90,11 @@ 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; } } -- 2.20.1