Object Oriented Programming - Reset an Inherited Constructor Property

Im a bit confused on this question. I understand we ned to explictly set the constructor for the two prototypes but wouldn’t the soloution be the same as:

function Animal() { }
function Bird() { }

Bird.prototype = {
  constructor: Bird,
};

let bird = new Bird();

console.log(bird.constructor); // Output: Bird

Code solution*

function Animal() { }
function Bird() { }
function Dog() { }

Bird.prototype = Object.create(Animal.prototype);
Dog.prototype = Object.create(Animal.prototype);

// Only change code below this line

Bird.prototype.constructor = Bird;
Dog.prototype.constructor = Dog;

let duck = new Bird();
let beagle = new Dog();

Your browser information:

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

Challenge: Object Oriented Programming - Reset an Inherited Constructor Property

Link to the challenge:

Short answer is it isn’t the same. You are manually replacing all the properties on the Bird prototype with just a constructor. The bird instance you created doesn’t really exist. It wasn’t a function call. All the bird variable has is the constructor variable you gave it.

You see every class in JavaScript extends Object. That’s what gives classes their basic methods.

Here’s all the properties the Bird prototype has before you reassign it.

Object { … }
​
constructor: function Bird()​
<prototype>: Object { … }
​​
__defineGetter__: function __defineGetter__()
​​
__defineSetter__: function __defineSetter__()
​​
__lookupGetter__: function __lookupGetter__()
​​
__lookupSetter__: function __lookupSetter__()
​​
__proto__: 
​​
constructor: function Object()
​​
hasOwnProperty: function hasOwnProperty()
​​
isPrototypeOf: function isPrototypeOf()
​​
propertyIsEnumerable: function propertyIsEnumerable()
​​
toLocaleString: function toLocaleString()
​​
toString: function toString()
​​
valueOf: function valueOf()
​​
<get __proto__()>: function __proto__()
​​
<set __proto__()>: function __proto__()

And here’s after.

{
  constructor: function Bird() { }
}

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