Tell us what’s happening:
Don’t understand how code works in lesson.
What code preceded these lines?
Your code so far
/* // if add such a code, it will be true, false, true
function Bird(name) {
this.name = name;
}
let duck = new Bird("Donald");
*/
duck.constructor === Bird;
duck.constructor === Object;
duck instanceof Bird;
// on site: In order, these expressions would evaluate to false, true, and true.
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:100.0) Gecko/20100101 Firefox/100.0
Challenge: Object Oriented Programming - Remember to Set the Constructor Property when Changing the Prototype
And then add a prototype which is an object like so
Bird.prototype = {
numLegs: 2,
eat: function() {
console.log("nom nom nom");
},
describe: function() {
console.log("My name is " + this.name);
}
};
It takes the “constructor” property from that first bird constructor. That’s why duck.constructor = false.
When you make a prototype object, you have to add the constructor property back in; with that original constructor’s name “Bird” as the property’s value
{ constructor: Bird }
Bird.prototype = {
constructor: Bird,
numLegs: 2,
eat: function() {
console.log("nom nom nom");
},
describe: function() {
console.log("My name is " + this.name);
}
};
This is an excellent explanation. However I find one part still confusing.
As you rightly explained, if you overwrite Bird.prototype with a new object literal, you end up deleting the constructor property and hence have to manually re-enter the constructor property.
That said, how does (duck instanceof Bird) still return “true” ??
Especially as the constructor property has been deleted from Bird.
What property does the “instanceof” operator check?