Extending Object.prototype breaks JavaScript for..in loops, which are used heavily in Google Maps API v3

I am developing project in meteor. I need to find object key form object value. So I tried the below code, But browser freezed.

Object.prototype.getKeyByValue = function( value ) {
    for( var prop in this ) {
        if( this.hasOwnProperty( prop ) ) {
             if( this[ prop ] === value )
                 return prop;
        }
    }
}

var test = {
   key1: 42,
   key2: 'foo'
};

test.getKeyByValue( 42 );  // returns 'key1'

console log

This site adds property <getKeyByValue> to Object.prototype. Extending Object.prototype breaks JavaScript for..in loops, which are used heavily in Google Maps API v3


Source: javascript

Leave a Reply