2018-10-10 22:03:03 +00:00
---
id: 587d7db1367417b2b2512b86
2021-02-06 04:42:36 +00:00
title: Reset an Inherited Constructor Property
2018-10-10 22:03:03 +00:00
challengeType: 1
2020-08-04 07:15:28 +00:00
forumTopicId: 301324
2021-01-13 02:31:00 +00:00
dashedName: reset-an-inherited-constructor-property
2018-10-10 22:03:03 +00:00
---
2020-12-16 07:37:30 +00:00
# --description--
2021-02-06 04:42:36 +00:00
When an object inherits its `prototype` from another object, it also inherits the supertype's constructor property.
2020-12-16 07:37:30 +00:00
2021-02-06 04:42:36 +00:00
Here's an example:
2020-08-04 07:15:28 +00:00
```js
function Bird() { }
Bird.prototype = Object.create(Animal.prototype);
let duck = new Bird();
duck.constructor // function Animal(){...}
```
2021-02-06 04:42:36 +00:00
But `duck` and all instances of `Bird` should show that they were constructed by `Bird` and not `Animal` . To do so, you can manually set `Bird's` constructor property to the `Bird` object:
2020-08-04 07:15:28 +00:00
```js
Bird.prototype.constructor = Bird;
duck.constructor // function Bird(){...}
```
2020-12-16 07:37:30 +00:00
# --instructions--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
Fix the code so `duck.constructor` and `beagle.constructor` return their respective constructors.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
# --hints--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`Bird.prototype` should be an instance of `Animal` .
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert(Animal.prototype.isPrototypeOf(Bird.prototype));
2018-10-10 22:03:03 +00:00
```
2021-02-06 04:42:36 +00:00
`duck.constructor` should return `Bird` .
2018-10-10 22:03:03 +00:00
```js
2020-12-16 07:37:30 +00:00
assert(duck.constructor === Bird);
2018-10-10 22:03:03 +00:00
```
2021-02-06 04:42:36 +00:00
`Dog.prototype` should be an instance of `Animal` .
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert(Animal.prototype.isPrototypeOf(Dog.prototype));
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`beagle.constructor` should return `Dog` .
2020-08-04 07:15:28 +00:00
2018-10-10 22:03:03 +00:00
```js
2020-12-16 07:37:30 +00:00
assert(beagle.constructor === Dog);
2018-10-10 22:03:03 +00:00
```
2020-08-04 07:15:28 +00:00
2021-01-13 02:31:00 +00:00
# --seed--
## --seed-contents--
```js
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();
```
2020-12-16 07:37:30 +00:00
# --solutions--
2021-01-13 02:31:00 +00:00
```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();
```