Override Inherited Methods
In previous lessons, you learned that an object can inherit its behavior (methods) from another object by referencing its prototype
object:
ChildObject.prototype = Object.create(ParentObject.prototype);
Then the ChildObject
received its own methods by chaining them onto its prototype
:
ChildObject.prototype.methodName = function() {...};
It's possible to override an inherited method. It's done the same way - by adding a method to ChildObject.prototype
using the same method name as the one to override. Here's an example of Bird
overriding the eat()
method inherited from Animal
:
function Animal() { }
Animal.prototype.eat = function() {
return "nom nom nom";
};
function Bird() { }
Bird.prototype = Object.create(Animal.prototype);
Bird.prototype.eat = function() {
return "peck peck peck";
};
If you have an instance let duck = new Bird();
and you call duck.eat()
, this is how JavaScript looks for the method on the prototype
chain of duck
:
duck
=> Iseat()
defined here? No.Bird
=> Iseat()
defined here? => Yes. Execute it and stop searching.Animal
=>eat()
is also defined, but JavaScript stopped searching before reaching this level.- Object => JavaScript stopped searching before reaching this level.
Override the fly()
method for Penguin
so that it returns the string Alas, this is a flightless bird.
Tests
- Waiting: 1.
penguin.fly()
should return the stringAlas, this is a flightless bird.
- Waiting: 2. The
bird.fly()
method should return the stringI am flying!
/** * Your test output will go here */