2.9 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7db1367417b2b2512b87 | Añade métodos después de la herencia | 1 | 301315 | add-methods-after-inheritance |
--description--
Una función constructor que hereda su objeto prototype
de una función constructor "supertype" puede seguir teniendo sus propios métodos además de los heredados.
Por ejemplo, Bird
es un constructor que hereda su prototype
de Animal
:
function Animal() { }
Animal.prototype.eat = function() {
console.log("nom nom nom");
};
function Bird() { }
Bird.prototype = Object.create(Animal.prototype);
Bird.prototype.constructor = Bird;
Además de lo que se hereda de Animal
, se quiere añadir un comportamiento que sea exclusivo de los objetos Bird
. Aquí, Bird
obtendrá una función fly()
. Las funciones se añaden al prototype
de Bird's
del mismo modo que cualquier función constructor:
Bird.prototype.fly = function() {
console.log("I'm flying!");
};
Ahora las instancias de Bird
tendrán métodos tanto eat()
como fly()
:
let duck = new Bird();
duck.eat();
duck.fly();
duck.eat()
mostrará la cadena nom nom nom
en consola, y duck.fly()
mostrará la cadena I'm flying!
.
--instructions--
Añade el código necesario para que el objeto Dog
herede de Animal
y el constructor prototype
de Dog
sea establecido en Dog
. A continuación agrega el método bark()
al objeto Dog
, para que beagle
pueda "comer" eat()
y "ladrar" bark()
. El método bark()
debe imprimir Woof!
por consola.
--hints--
Animal
no debe responder al método bark()
.
assert(typeof Animal.prototype.bark == 'undefined');
Dog
debe heredar el método eat()
de Animal
.
assert(typeof Dog.prototype.eat == 'function');
El prototipo Dog
debe tener un método bark()
.
assert('bark' in Dog.prototype);
beagle
debe ser una instancia de (instanceof
) Animal
.
assert(beagle instanceof Animal);
El constructor para beagle
debe establecerse en Dog
.
assert(beagle.constructor === Dog);
beagle.eat()
debe imprimir la cadena nom nom nom
console.log = function (msg) {
throw msg;
};
assert.throws(() => beagle.eat(), 'nom nom nom');
beagle.bark()
debe imprimir la cadena Woof!
console.log = function (msg) {
throw msg;
};
assert.throws(() => beagle.bark(), 'Woof!');
--seed--
--seed-contents--
function Animal() { }
Animal.prototype.eat = function() { console.log("nom nom nom"); };
function Dog() { }
// Only change code below this line
// Only change code above this line
let beagle = new Dog();
--solutions--
function Animal() { }
Animal.prototype.eat = function() { console.log("nom nom nom"); };
function Dog() { }
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.bark = function () {
console.log('Woof!');
};
let beagle = new Dog();
beagle.eat();
beagle.bark();