2.6 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 587d7db1367417b2b2512b88 | Sobrescribir métodos heredados | 1 | 301322 | override-inherited-methods |
--description--
En lecciones anteriores, aprendiste que un objeto puede heredar su comportamiento (métodos) de otro objeto al referenciar su prototype:
ChildObject.prototype = Object.create(ParentObject.prototype);
Luego, el ChildObject recibió sus propios métodos al encadenarlos a su prototype:
ChildObject.prototype.methodName = function() {...};
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:
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";
};
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:
duck=> ¿Estáeat()definido aquí? No.Bird=> ¿Estáeat()definido aquí? => Sí. Ejecútala y detén la búsqueda.Animal=>eat()también está definido, pero JavaScript dejó de buscar antes de llegar a este nivel.- Object => JavaScript dejó de buscar antes de llegar a este nivel.
--instructions--
Sobrescribe el método fly() para Penguin de manera que devuelva la cadena de texto Alas, this is a flightless bird.
--hints--
penguin.fly() debe devolver la cadena de texto Alas, this is a flightless bird.
assert(penguin.fly() === 'Alas, this is a flightless bird.');
El método bird.fly() debe devolver la cadena de texto I am flying!
assert(new Bird().fly() === 'I am flying!');
--seed--
--seed-contents--
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--
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());