Exercise “Add methods After Inheritance” in the Object Oriented Programming section asks for Dog to have its own property “bark”, but what it really wants is for Dog.prototype to have its own property “bark”. Trying to put the property “bark” on Dog will not help to pass the exercise.
the instructions are
Add all necessary code so the
Dog
object inherits fromAnimal
and theDog's
prototype
constructor is set to Dog. Then add abark()
method to theDog
object so thatbeagle
can botheat()
andbark()
. Thebark()
method should print “Woof!” to the console.
As you can’t type inside the constructor Dog
function (there are the comments that tell you where to write) you need to create the new method using the prototype
keyword, and that is adding a property to the Dog
object.
Object Oriented Programming is a difficult subject, it needs a lot to learn the basics well - not surrender now!
Thanks for responding! My issue is the wording of this goal. saying that it should be an “own” property made me test with
console.log(Dog.hasOwnProperty("bark"));
which returns true if you do Dog.bark = function(){...
but not if you attach bark
to Dog’s prototype. The goal should be reworded to reflect this.
If you check the documentation about hasOwnProperty()
All descendents of
Object
inherit thehasOwnProperty
method. This method can be used to determine whether an object has the specified property as a direct property of that object; unlike thein
operator, this method does not check for a property in the object’s prototype chain.
Try with the in
operator mentioned above
I understand this, however it still doesn’t make bark
an “own” property. See the ECMAScript 2018 specification for own properties, and below it, inherited properties. It says that properties of prototypes should be considered “inherited”, not “own”. I can’t post the link here, but googling ECMAScript 2018 Language Specification should bring you there.