OOP: Iterate Over All Properties

Hey guys,
Could someone explain to me what is going on in this code in plain English? I get that it is iterating over the properties and pushing it to an array but can you walk me through the code?
Not looking for the solution. Just trying to understand the code better.

let ownProps = [];
let prototypeProps = [];

for (let property in duck) {
  if(duck.hasOwnProperty(property)) { //the hasOwnProperty(property) confuses me
    ownProps.push(property); 
  } else {
    prototypeProps.push(property);
  }
}

Thank you in advance. I appreciate your help.

1 Like

there are two things going on here:
a for...in loop, this iterates over the property of the object, including the properties in the prototype chain
the hasOwnProperty method returns true only if the property is a property of the object itself, and not of the prototype chain

so the for...in property is getting all the property names, including properties of the prototype, and the hasOwnProperty method is distinguishing between properties belonging directly to the object, and properties belonging to the prototype chain

1 Like

Thank you, that was very helpful!