I played with this concept a little, and I found out that on browser console, manually setting the prototype property to a new object doesn’t require you to reset the constructor property.
Duck.constructor === Bird
is true
both before and after setting Bird.prototype
to new object
Is this only because of the difference between the FCC console and Browser Console or is there more to why this is?
Did you try the same in FCC, you will get the same output…
I believe the reason is this one (the order is important):
- You create a Duck - I wouldn’t use Uppercase -
- So a prototype is associated with Duck,
- You reset Bird Prototype
- To update Duck prototype, you have to call Duck = new Bird(…) again
I wrote a similar example on Node
function Bird(){}
> undefined
let Duck = new Bird
> undefined
Bird.prototype = {}
> {}
Duck.constructor == Bird
> true
Duck = new Bird
> {}
Duck.constructor == Bird
> false
1 Like
Thanks for the Pointing out the Uppercase, Its bad practice, I’ll try to avoid using it like that. But I don’t understand your explanation
The prototype isn’t updated unless you re-assign Duck = new Bird
1 Like
Do you mean Bird.prototype = { numLegs : 2}
isn’t updated or trying to reset the prototype with Duck.prototype.constructor
doesn’t work until I create a new instance of Bird? (I’m sorry if this question sounds silly, I really don’t understand)
Yes, exactly. You can try with numLegs, and it should be the same.
It’s not silly!
1 Like
Thanks Thanks I get it now
1 Like