Understand Own Properties hasOwnProperty() problem

Tell us what’s happening:

Why in the example given in the explanation of the challenge use the hasOwnProperty() method? As far as I know, there is nothing to verify, we just want to store the object properties in an array, i.e. the hasOwnProperty() method is not necessary.

I would appreciate it if you could clarify this for me, thank you.

Your code so far


function Bird(name) {
  this.name = name;
  this.numLegs = 2;
}

let canary = new Bird("Tweety");
let ownProps = [];
// Add your code below this line
for (let prop in canary) {
  if(canary.hasOwnProperty(prop)){
    ownProps.push(prop);
  }
}



Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36.

Challenge: Understand Own Properties

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/object-oriented-programming/understand-own-properties

Complete the following two lessons and it should make more sense why you use the hasOwnProperty method here.

2 Likes

BROOOOOOOOO, I got it in the Iterate Over All Properties challenge. You are the man!!!