Prototypes as an object not working

I’m trying to complete this challenge by creating a Dog.prototype object and I don’t understand why it’s not working.

The code I tried was

Dog.prototype = Object.create(Animal.prototype);
Dog.prototype = {
    constructor: Dog,
    bark: function() {
        console.log("Woof!");
    }
};

but when I do this, the error messages I get are:

  • Dog should inherit the eat() method from Animal.
  • beagle should be an instanceof Animal.
  • beagle.eat() should log “nom nom nom”

It seems to only work when I add each property to the prototype individually, like:

Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.bark = function() { console.log("Woof!") };

Why is this? And if I’m doing something wrong with my prototype object, how would I correctly complete this challenge by creating an object prototype rather than adding the properties individually? Thank you!

Hi,

The = operator means assign right hand value to the left.
So you basically assigning new value to Dog.prototype, not adding new property.

Hi, thank you for the reply but I’m still pretty confused. I’m trying to use the principle taught from an earlier challenge that says it’s cleaner and more efficient to create the prototype as an object. The syntax that worked for this challenge is exactly the same as what I tried for the challenge my original post was about, yet it doesn’t work for this one?

If you want to add new property to one object while keep the old property, you can combined it with Object.assign
Object.assign() - JavaScript | MDN (mozilla.org)

So something like:

Dog.prototype = Object.assign(
    Object.create(Animal.prototype),
   {
      constructor: Dog,
      bark: () => console.log('Woof!')
   }
)

Oh I see, thank you! Would you mind explaining it a bit more so I can understand it more thoroughly? Is it like the initial Dog.prototype object I created overwrites the Dog.prototype=Object.create(Animal.prototype); line before it, so I need to use the Object.assign() function to combine it rather than overwriting it? Is this always necessary for specific cases where I’m trying to use inheritance, and that’s why I didn’t need to do this for the earlier challenge?

When you create new variable like this:

var amy = {
  firstName: 'amy',
  lastName: 'anders'
};
amy = {
  age: 24,
  address: 'Somewhere'
}
console.log(amy)
// the value will be { age: 24, address: 'Somewhere' }

See you not ADDING property, but assign new value to amy.

So in your code above, you giving Dog.prototype a new value, not adding new property.

If you want to add new property to an object, than you have to do it manually:

var amy = {
  firstName: 'amy',
  lastName: 'anders'
};

amy.age = 24;
amy.address = 'Somewhere'

// or

var otherAmyData = {
  age: 24,
  address: 'Somewhere'
}

amy = Object.assign(amy, otherAmyData)

//with ES2018 syntax: 
amy = {...amy, otherAmyData}

About that three dots: Spread syntax (…) - JavaScript | MDN (mozilla.org)

In earlier challenge, you initialize new VALUE to the object, not adding new property.

Hope it help.

Ohhhh okay makes sense, thank you so much! I really appreciate the help :slight_smile:

1 Like