120

I have the following code:

  for (i in awards) {
         if (awards[i] instanceof Array === false) {
               console.log(awards[i]);
                httpFactory.patch(awards[i], {"read": true}, false);
             }
       }

My IDE shows this error relating to the code above:

Possible iteration over unexpected (custom / inherited) members, probably missing hasOwnProperty check

Checks for any instances of unfiltered for-in loops in JavaScript. The use of this construct results in processing inherited or unexpected properties. You need to filter own properties with hasOwnProperty() method. The validation works in JavaScript, html or jsp files.

Could you explain in more detail what is meant by this statement?

Prometheus
  • 32,405
  • 54
  • 166
  • 302
  • 3
    jQuery doesn't perform `hasOwnProperty` checks - I wonder how many warnings that would produce.... – Alnitak Sep 08 '14 at 12:01

5 Answers5

187

The IDE is recommending that you add a test:

if (awards.hasOwnProperty(i)) {
    ...
}

inside the for loop.

I personally recommend not doing this, and disabling the warning if possible. There's simply no need in most code, and even less need in ES5 code where you can safely add non-enumerable properties to an object using Object.defineProperty

The hasOwnProperty check is only necessary if you have unsafely added new (enumerable) properties to Object.prototype, so the simplest fix is don't do that.

jQuery doesn't perform this test - they explicitly document that jQuery will break if Object.prototype is unsafely modified.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • 60
    In IntelliJ 15, to disable the warning, perform the following: Open Preferences -> Editor -> Code Style -> Inspections. In the search box, enter "hasOwnProperty". It will display "Unfiltered for..in loop". Uncheck that box. Click OK button to accept change and close the window. – Machtyn Mar 25 '16 at 14:50
  • 2
    In IntelliJ: CTRL ALT S (unless you're using Ubuntu which has its own CTRL ALT S shortcut) just search for "hasOwnProperty" and you'll get straight to the problem, uncheck it and it's done! – Olivier Pons Sep 20 '17 at 07:15
  • 3
    In WebStorm, go to File...Settings, then Editor -> Inspections -> "JavaScript and TypeScript" -> General, and uncheck "Unfiltered for..in loop". – mightypile Apr 22 '20 at 14:29
  • @Machtyn There is no need to go to settings to disable this warning. On Linux/Windows, press Alt + Enter with the cursor over the warning, select the first option on the context menu that popups and disable the setting in the submenu next to the first option. – Peter Chaula Nov 19 '20 at 13:34
29

Every object in javascript has prototype which has its own properties(native/inherited methods/properties) and properties which are directly attached to object itself.

When you iterate over an object, it will iterate the properties of the object itself and the properties of the prototype of the object.

So, In order to avoid iterating over the prototype, it is recommended to use hasOwnProperty method which return true only when the object has the mentioned property directly. i.e, Not inside prototype

Example

for (var k in object) {
  if (object.hasOwnProperty(k)) {
     // do your computation here.
  }
}

More details can be found here

Community
  • 1
  • 1
Selvaraj M A
  • 3,096
  • 3
  • 30
  • 46
27

You can also refactor your loop into:

const keys = Object.keys(object);
for (const key of keys){
   // do something with object[key];
}
Flavien Volken
  • 19,196
  • 12
  • 100
  • 133
17

Also you can get rid of the warning writing a forEach loop for a more readable and functional approach:

Object.keys(object).forEach(key => {
    // Do something with object[key]
});
jnd0
  • 356
  • 3
  • 11
-4

you should add one more condition at the beginning of this loop

if (awards.hasOwnProperty(i)) 
bmazurek
  • 169
  • 4
  • 16
    I voted this answer down, so I think it's polite if I say why. This answer says how to avoid the warning but it doesn't explain what the warning means, how this avoids it, or what the this code does. – WoodenKitty Dec 21 '16 at 00:02