In the cause of trying to reset an inherited constructor property, why was Bird used instead of Bird() since Bird is a function?

Tell us what’s happening:
why this
Bird.prototype.constructor = Bird;
instead of this
Bird.prototype.constructor = Bird();

Your code so far


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();
console.log(duck instanceof Bird);
let beagle = new Dog();

Your browser information:

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

Challenge: Reset an Inherited Constructor Property

Link to the challenge:

What is the value of duck in the above line of code. A function or an object?

sorry the answer was true.
but if i do Bird.prototype.constructor = new Bird()
followed by let duck = new Bird()
then console.log(duck.Bird()) will output undefine

Nope. The value of duck is supposed to be {} unless you are invoking it like an ordinary function without new keyword. But if you invoke it as a constructor like new Bird() then it should return {}.

let duck = new Bird();
let beagle = new Dog();
console.log(duck)

My point is, Bird.prototype.constructor is supposed to be a function. If you do Bird.prototype.constructor = Bird(); it will point to undefined as you have seen and Bird.prototype.constructor = new Bird(); will make it point to {}. Therefore for it to point to Bird which is a function, you should have Bird.prototype.constructor = Bird;.

1 Like

I remember getting confused about this when I was first learning programming:

function myFunction () {
}

myFunction a reference to a function. It’s the variable that function is assigned to.

myFunction() is the result of executing that function, it’s a value of some kind (whatever the function returns).

So Bird is [a reference to] the constructor (a function that returns an object). Bird() isn’t the constructor, it’s the value that gets returned when you run the constructor function (which is undefined in this case, because constructors need to be invoked like new Bird() to actually return an object).

1 Like