2021-02-06 04:42:36 +00:00
---
id: 587d7db1367417b2b2512b88
2021-03-29 22:47:35 +09:00
title: Sobrescribir métodos heredados
2021-02-06 04:42:36 +00:00
challengeType: 1
forumTopicId: 301322
dashedName: override-inherited-methods
---
# --description--
2021-03-29 22:47:35 +09:00
En lecciones anteriores, aprendiste que un objeto puede heredar su comportamiento (métodos) de otro objeto al referenciar su `prototype` :
2021-02-06 04:42:36 +00:00
```js
ChildObject.prototype = Object.create(ParentObject.prototype);
```
2021-03-29 22:47:35 +09:00
Luego, el `ChildObject` recibió sus propios métodos al encadenarlos a su `prototype` :
2021-02-06 04:42:36 +00:00
```js
ChildObject.prototype.methodName = function() {...};
```
2021-03-29 22:47:35 +09:00
Es posible sobreescribir un método heredado. Se hace de la misma manera: agregando un método a `ChildObject.prototype` usando el mismo nombre de método que el que se va a sobrescribir. Aquí hay un ejemplo de `Bird` sobrescribiendo el método `eat()` heredado de `Animal` :
2021-02-06 04:42:36 +00:00
```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";
};
```
2021-06-20 22:29:09 +05:30
Si tienes una instancia de `let duck = new Bird();` y llamas a `duck.eat()` , así es como JavaScript busca el método en la cadena `prototype` de `duck` :
2021-02-06 04:42:36 +00:00
2021-03-29 22:47:35 +09:00
1. `duck` => ¿Está `eat()` definido aquí? No.
2021-06-20 22:29:09 +05:30
2. `Bird` => ¿Está `eat()` definido aquí? => Sí. Ejecútala y detén la búsqueda.
2021-03-29 22:47:35 +09:00
3. `Animal` => `eat()` también está definido, pero JavaScript dejó de buscar antes de llegar a este nivel.
4. Object => JavaScript dejó de buscar antes de llegar a este nivel.
2021-02-06 04:42:36 +00:00
# --instructions--
2021-03-29 22:47:35 +09:00
Sobrescribe el método `fly()` para `Penguin` de manera que devuelva la cadena de texto `Alas, this is a flightless bird.`
2021-02-06 04:42:36 +00:00
# --hints--
2021-03-29 22:47:35 +09:00
`penguin.fly()` debe devolver la cadena de texto `Alas, this is a flightless bird.`
2021-02-06 04:42:36 +00:00
```js
assert(penguin.fly() === 'Alas, this is a flightless bird.');
```
2021-03-29 22:47:35 +09:00
El método `bird.fly()` debe devolver la cadena de texto `I am flying!`
2021-02-06 04:42:36 +00:00
```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());
```