2.7 KiB
2.7 KiB
id | title | localeTitle | challengeType |
---|---|---|---|
587d7db1367417b2b2512b86 | Reset an Inherited Constructor Property | Restablecer una propiedad de constructor heredada | 1 |
Description
prototype
de otro objeto, sino que también hereda el supertype
propiedad constructor 's.
Aquí hay un ejemplo:
function Bird() { }Pero el
Bird.prototype = Object.create(Animal.prototype);
let duck = new Bird();
duck.constructor // function Animal(){...}
duck
y todos los casos de Bird
deben mostrar que fueron construidos por Bird
y no por Animal
. Para hacerlo, puedes establecer manualmente Bird's
propiedad Bird's
constructor de Bird
objeto Bird
:
Bird.prototype.constructor = Bird;
duck.constructor // function Bird(){...}
Instructions
duck.constructor
el código para que duck.constructor
y beagle.constructor
devuelvan sus respectivos constructores.
Tests
tests:
- text: <code>Bird.prototype</code> debe ser una instancia de <code>Animal</code> .
testString: 'assert(Animal.prototype.isPrototypeOf(Bird.prototype), "<code>Bird.prototype</code> should be an instance of <code>Animal</code>.");'
- text: <code>duck.constructor</code> debe devolver <code>Bird</code> .
testString: 'assert(duck.constructor === Bird, "<code>duck.constructor</code> should return <code>Bird</code>.");'
- text: <code>Dog.prototype</code> debe ser una instancia de <code>Animal</code> .
testString: 'assert(Animal.prototype.isPrototypeOf(Dog.prototype), "<code>Dog.prototype</code> should be an instance of <code>Animal</code>.");'
- text: <code>beagle.constructor</code> debe devolver el <code>Dog</code> .
testString: 'assert(beagle.constructor === Dog, "<code>beagle.constructor</code> should return <code>Dog</code>.");'
Challenge Seed
function Animal() { }
function Bird() { }
function Dog() { }
Bird.prototype = Object.create(Animal.prototype);
Dog.prototype = Object.create(Animal.prototype);
// Add your code below this line
let duck = new Bird();
let beagle = new Dog();
Solution
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();