101 lines
2.5 KiB
Markdown
101 lines
2.5 KiB
Markdown
![]() |
---
|
||
|
id: 587d7db1367417b2b2512b88
|
||
|
title: Override Inherited Methods
|
||
|
challengeType: 1
|
||
|
forumTopicId: 301322
|
||
|
dashedName: override-inherited-methods
|
||
|
---
|
||
|
|
||
|
# --description--
|
||
|
|
||
|
In previous lessons, you learned that an object can inherit its behavior (methods) from another object by referencing its `prototype` object:
|
||
|
|
||
|
```js
|
||
|
ChildObject.prototype = Object.create(ParentObject.prototype);
|
||
|
```
|
||
|
|
||
|
Then the `ChildObject` received its own methods by chaining them onto its `prototype`:
|
||
|
|
||
|
```js
|
||
|
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`:
|
||
|
|
||
|
```js
|
||
|
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`:
|
||
|
|
||
|
1. `duck` => Is `eat()` defined here? No.
|
||
|
2. `Bird` => Is `eat()` defined here? => Yes. Execute it and stop searching.
|
||
|
3. `Animal` => `eat()` is also defined, but JavaScript stopped searching before reaching this level.
|
||
|
4. Object => JavaScript stopped searching before reaching this level.
|
||
|
|
||
|
# --instructions--
|
||
|
|
||
|
Override the `fly()` method for `Penguin` so that it returns the string `Alas, this is a flightless bird.`
|
||
|
|
||
|
# --hints--
|
||
|
|
||
|
`penguin.fly()` should return the string `Alas, this is a flightless bird.`
|
||
|
|
||
|
```js
|
||
|
assert(penguin.fly() === 'Alas, this is a flightless bird.');
|
||
|
```
|
||
|
|
||
|
The `bird.fly()` method should return the string `I am flying!`
|
||
|
|
||
|
```js
|
||
|
assert(new Bird().fly() === 'I am flying!');
|
||
|
```
|
||
|
|
||
|
# --seed--
|
||
|
|
||
|
## --seed-contents--
|
||
|
|
||
|
```js
|
||
|
function Bird() { }
|
||
|
|
||
|
Bird.prototype.fly = function() { return "I am flying!"; };
|
||
|
|
||
|
function Penguin() { }
|
||
|
Penguin.prototype = Object.create(Bird.prototype);
|
||
|
Penguin.prototype.constructor = Penguin;
|
||
|
|
||
|
// Only change code below this line
|
||
|
|
||
|
|
||
|
|
||
|
// Only change code above this line
|
||
|
|
||
|
let penguin = new Penguin();
|
||
|
console.log(penguin.fly());
|
||
|
```
|
||
|
|
||
|
# --solutions--
|
||
|
|
||
|
```js
|
||
|
function Bird() { }
|
||
|
|
||
|
Bird.prototype.fly = function() { return "I am flying!"; };
|
||
|
|
||
|
function Penguin() { }
|
||
|
Penguin.prototype = Object.create(Bird.prototype);
|
||
|
Penguin.prototype.constructor = Penguin;
|
||
|
Penguin.prototype.fly = () => 'Alas, this is a flightless bird.';
|
||
|
let penguin = new Penguin();
|
||
|
console.log(penguin.fly());
|
||
|
```
|