Prototype Properties to Reduce Duplicate Code-doubt

Tell us what’s happening:
hi there, in code as you will see i used hasOwnProperty to determine whether the “numLegs”, is the property of the new object/instance beagle, i think it should show “true” or maybe “false” but it is showing “undefined”, does it have to do something with the use of 'protoptype ’ or i have made some mistake here.
thnx, constructive criticism appreciated

Your code so far


function Dog(name) {
  this.name = name;
  Dog.prototype.numLegs=4;
}



// Add your code above this line
let beagle = new Dog("Snoopy");
console.log(beagle.hasOwnProperty(numLegs));

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/object-oriented-programming/use-prototype-properties-to-reduce-duplicate-code

The reason you are getting that error message is because hasOwnProperty is expecting a string value. numLegs is not a string, because you never declared a variable named numLegs.

FYI - The prototype declaration should be made outside of the Dog function.

Ok, so now it is showing me false, is it because it has something to do with prototype ?

Even if he does the following:

console.log( beagle.hasOwnProperty('numLegs') ); the output will be false

But if he tries the following:

console.log( Dog.prototype.hasOwnProperty('numLegs') ); he will get true

Is that sufficient to conclude that hasOwnProperty doesn’t work when a property is defined in a prototype and that the instance doesn’t have it, but it searches for it / and inherit it from the prototype?

I think it’s a yes.

The property is in the prototype and it’s only inherited.

In some YouTube videos, they try to explain that the instance searches for the property if it’s defined in a prototype… I don’t know if that is true, or they explain it that way to make it easier to understand. But that partly explains why hasOwnProperty doesn’t work when invoked with an instance of a class which means that the property isn’t physically present in the instance.

Independently from its mechanism and how it works, the most important thing is that we know now that it doesn’t work because of prototype.

Edit: I also tried Object.keys() and I got the same result, numLegs was not recognized as a native property of beagle: console.log( Object.keys(beagle) ); // ouput: name