Why doesn't my Constructor Prototype work?

Shouldn’t a numLegs property exist for my beagle object? Please help!

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

// Only change code above this line
let beagle = new Dog("Snoopy");

console.log(beagle); // Prints { name: 'Snoopy' }

console.log(Object.getOwnPropertyNames(beagle)) // Print [ 'name' ]

Try out your code in the browser console and you’ll see where numLegs is:
You can definitely access numLegs via beagle.numLegs as the property is up in the prototype chain.

Further reading: Inheritance and the prototype chain - JavaScript | MDN (mozilla.org)

… the func.prototype property of functions, which instead specifies the [[Prototype]] to be assigned to all instances of objects created by the given function when used as a constructor…

Note modern browsers provide access to the hidden [[Prototype]] object via __proto__.


js Prototype

This will help you understand the prototype chain

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.