Iterate Over All Properties and parent object

Hello,

I looked at the hint and solution (below) to the challenge " Object Oriented Programming: Iterate Over All Properties" but I still don’t understand. Why iterating through the “beagle” object and then check its parent “Dog” in the solution?

Your code so far


function Dog(name) {
  this.name = name;
}

Dog.prototype.numLegs = 4;

let beagle = new Dog("Snoopy");

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

// Add your code below this line 
for (let prop in beagle) {
  if (Dog.hasOwnProperty(prop)) {
    ownProps.push(prop);
  }
  else {
    prototypeProps.push(prop);
  }
}

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/object-oriented-programming/iterate-over-all-properties/

I can see where this would be confusing.

In that solution they are checking which properties of beagle are on Dog and assuming (else) any others are on the prototype.

You could also check which properties are on beagle object specifically and assume that any others are on the prototype.

for(let prop in beagle){
  if(beagle.hasOwnProperty(prop)){
    ownProps.push(prop);
  }else{
    prototypeProps.push(prop)
  }
}

In the case of the challenge either will yield the correct answer and pass. I suppose the right way depends on what you really want know about - Dog or beagle

I’m thinking I would opt for checking properties of beagle specifically. In this case below the prototype property of lucky is overriden and now that particular Dog has an ownProperty of numLegs.

let lucky = new Dog("Lucky");
lucky.numLegs = 3;  // overriding the prototype - poor Lucky :(

// checking lucky  properties
for(let prop in lucky){
  if(lucky.hasOwnProperty(prop)){
    ownProps.push(prop);
    console.log("own", prop, lucky[prop])
  }else{
    prototypeProps.push(prop)
    console.log("proto", prop, lucky[prop])
  }
}
console.log(lucky);

// own name Lucky
// own numLegs 3 - prototype overridden
// Dog { name: 'Lucky', numLegs: 3 }

Checking Dog would not catch that numLegs was not a prototype property for lucky.



// checking Dog properties
for(let prop in lucky){
  if(Dog.hasOwnProperty(prop)){
    ownProps.push(prop);
    console.log("own", prop, lucky[prop])
  }else{
    prototypeProps.push(prop)
    console.log("proto", prop, lucky[prop])
  }
}
console.log(lucky)

// own name Lucky
// proto numLegs 3 - did not catch that numLegs was directly on lucky!
// Dog { name: 'Lucky', numLegs: 3 }


for(let prop in beagle){
  if(Dog.hasOwnProperty(prop)){
    ownProps.push(prop);
    console.log("own", prop, beagle[prop])
  }else{
    prototypeProps.push(prop)
    console.log("proto", prop, beagle[prop])
  }
}
console.log(beagle)

// own name Snoopy
// proto numLegs 4 - not overridden
// Dog { name: 'Snoopy' }

Thanks for the detailed answer, I asked first because

for(let prop in beagle){
  if(beagle.hasOwnProperty(prop)){
    ownProps.push(prop);
  }else{
    prototypeProps.push(prop)
  }
}

Did not pass the test, I had to check the hint and replace beagle with Dog in the first if check. i guess with more challenges I’ll eventually understand the intricacies of prototypes and properties.

Hmmmm…

That code above passed for me. It is also mirrors the example code exactly.

I can’t imagine why it would not pass for you.

I tried it again and now it passed, strange indeed.