freeCodeCamp/curriculum/challenges/spanish/02-javascript-algorithms-an.../object-oriented-programming/add-methods-after-inheritan...

3.7 KiB

id title challengeType videoUrl localeTitle
587d7db1367417b2b2512b87 Add Methods After Inheritance 1 Añadir métodos después de la herencia

Description

Una función constructora que hereda su objeto prototype de una función constructora de supertype aún puede tener sus propios métodos además de los métodos heredados. Por ejemplo, Bird es un constructor que hereda su prototype de Animal :
función animal () {}
Animal.prototype.eat = function () {
console.log ("nom nom nom");
};
función Bird () {}
Bird.prototype = Object.create (Animal.prototype);
Bird.prototype.constructor = Bird;
Además de lo que se hereda de Animal , desea agregar un comportamiento que sea exclusivo de los objetos Bird . Aquí, Bird obtendrá una función fly() . Las funciones se agregan al prototype Bird's la misma manera que cualquier función de constructor:
Bird.prototype.fly = function () {
console.log ("Estoy volando!");
};
Ahora las instancias de Bird tendrán los métodos de eat() y fly() :
dejar pato = nuevo pájaro ();
duck.eat (); // imprime "nom nom nom"
duck.fly (); // impresiones "Estoy volando!"

Instructions

Agregue todo el código necesario para que el objeto Dog hereda de Animal y el constructor Dog's prototype Dog's esté configurado en Dog. Luego, agregue un método de bark() al objeto Dog para que el beagle pueda eat() y bark() . El método de la bark() debe imprimir "¡Guau!" a la consola.

Tests

tests:
  - text: <code>Animal</code> no debe responder al método de la <code>bark()</code> .
    testString: 'assert(typeof Animal.prototype.bark == "undefined", "<code>Animal</code> should not respond to the <code>bark()</code> method.");'
  - text: <code>Dog</code> debe heredar el método de <code>eat()</code> de <code>Animal</code> .
    testString: 'assert(typeof Dog.prototype.eat == "function", "<code>Dog</code> should inherit the <code>eat()</code> method from <code>Animal</code>.");'
  - text: <code>Dog</code> debe tener el método de la <code>bark()</code> como propiedad <code>own</code> .
    testString: 'assert(Dog.prototype.hasOwnProperty("bark"), "<code>Dog</code> should have the <code>bark()</code> method as an <code>own</code> property.");'
  - text: <code>beagle</code> debe ser un <code>instanceof</code> <code>Animal</code> .
    testString: 'assert(beagle instanceof Animal, "<code>beagle</code> should be an <code>instanceof</code> <code>Animal</code>.");'
  - text: El constructor para <code>beagle</code> debe establecer en <code>Dog</code> .
    testString: 'assert(beagle.constructor === Dog, "The constructor for <code>beagle</code> should be set to <code>Dog</code>.");'

Challenge Seed

function Animal() { }
Animal.prototype.eat = function() { console.log("nom nom nom"); };

function Dog() { }

// Add your code below this line




// Add your code above this line

let beagle = new Dog();

beagle.eat(); // Should print "nom nom nom"
beagle.bark(); // Should print "Woof!"

Solution

// solution required