freeCodeCamp/curriculum/challenges/espanol/02-javascript-algorithms-an.../object-oriented-programming/reset-an-inherited-construc...

1.8 KiB

id title challengeType forumTopicId dashedName
587d7db1367417b2b2512b86 Restablece una propiedad "constructor" heredada 1 301324 reset-an-inherited-constructor-property

--description--

Cuando un objeto hereda el prototype de otro objeto, también hereda la propiedad del constructor del supertipo.

Por ejemplo:

function Bird() { }
Bird.prototype = Object.create(Animal.prototype);
let duck = new Bird();
duck.constructor

Pero duck y todas las instancias de Bird deberían mostrar que fueron construidas por Bird y no Animal. Para ello, puedes establecer manualmente la propiedad del constructor de Bird al objeto Bird:

Bird.prototype.constructor = Bird;
duck.constructor

--instructions--

Corrige el código para que duck.constructor y beagle.constructor devuelvan sus constructores respectivos.

--hints--

Bird.prototype debe ser una instancia de Animal.

assert(Animal.prototype.isPrototypeOf(Bird.prototype));

duck.constructor debe devolver Bird.

assert(duck.constructor === Bird);

Dog.prototype debe ser una instancia de Animal.

assert(Animal.prototype.isPrototypeOf(Dog.prototype));

beagle.constructor debe devolver Dog.

assert(beagle.constructor === Dog);

--seed--

--seed-contents--

function Animal() { }
function Bird() { }
function Dog() { }

Bird.prototype = Object.create(Animal.prototype);
Dog.prototype = Object.create(Animal.prototype);

// Only change code below this line



let duck = new Bird();
let beagle = new Dog();

--solutions--

function Animal() { }
function Bird() { }
function Dog() { }
Bird.prototype = Object.create(Animal.prototype);
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Bird.prototype.constructor = Bird;
let duck = new Bird();
let beagle = new Dog();