2018-09-30 23:01:58 +01:00
---
id: 587d7db1367417b2b2512b86
title: Reset an Inherited Constructor Property
challengeType: 1
2020-05-21 17:31:25 +02:00
isHidden: false
2019-08-05 09:17:33 -07:00
forumTopicId: 301324
2018-09-30 23:01:58 +01:00
---
## Description
<section id='description'>
2019-10-27 15:45:37 -01:00
When an object inherits its <code>prototype</code> from another object, it also inherits the supertype's constructor property.
2018-09-30 23:01:58 +01:00
Here's an example:
2019-05-17 06:20:30 -07:00
```js
function Bird() { }
Bird.prototype = Object.create(Animal.prototype);
let duck = new Bird();
duck.constructor // function Animal(){...}
```
2018-09-30 23:01:58 +01:00
But <code>duck</code> and all instances of <code>Bird</code> should show that they were constructed by <code>Bird</code> and not <code>Animal</code>. To do so, you can manually set <code>Bird's</code> constructor property to the <code>Bird</code> object:
2019-05-17 06:20:30 -07:00
```js
Bird.prototype.constructor = Bird;
duck.constructor // function Bird(){...}
```
2018-09-30 23:01:58 +01:00
</section>
## Instructions
<section id='instructions'>
Fix the code so <code>duck.constructor</code> and <code>beagle.constructor</code> return their respective constructors.
</section>
## Tests
<section id='tests'>
```yml
2018-10-04 14:37:37 +01:00
tests:
- text: <code>Bird.prototype</code> should be an instance of <code>Animal</code>.
2019-07-24 02:32:04 -07:00
testString: assert(Animal.prototype.isPrototypeOf(Bird.prototype));
2018-10-04 14:37:37 +01:00
- text: <code>duck.constructor</code> should return <code>Bird</code>.
2019-07-24 02:32:04 -07:00
testString: assert(duck.constructor === Bird);
2018-10-04 14:37:37 +01:00
- text: <code>Dog.prototype</code> should be an instance of <code>Animal</code>.
2019-07-24 02:32:04 -07:00
testString: assert(Animal.prototype.isPrototypeOf(Dog.prototype));
2018-10-04 14:37:37 +01:00
- text: <code>beagle.constructor</code> should return <code>Dog</code>.
2019-07-24 02:32:04 -07:00
testString: assert(beagle.constructor === Dog);
2018-09-30 23:01:58 +01:00
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function Animal() { }
function Bird() { }
function Dog() { }
Bird.prototype = Object.create(Animal.prototype);
Dog.prototype = Object.create(Animal.prototype);
2020-03-08 07:46:28 -07:00
// Only change code below this line
2018-09-30 23:01:58 +01:00
let duck = new Bird();
let beagle = new Dog();
```
</div>
</section>
## Solution
<section id='solution'>
```js
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();
```
</section>