92 lines
1.9 KiB
Markdown
92 lines
1.9 KiB
Markdown
![]() |
---
|
||
|
id: 587d7db1367417b2b2512b86
|
||
|
title: Reset an Inherited Constructor Property
|
||
|
challengeType: 1
|
||
|
forumTopicId: 301324
|
||
|
dashedName: reset-an-inherited-constructor-property
|
||
|
---
|
||
|
|
||
|
# --description--
|
||
|
|
||
|
When an object inherits its `prototype` from another object, it also inherits the supertype's constructor property.
|
||
|
|
||
|
Here's an example:
|
||
|
|
||
|
```js
|
||
|
function Bird() { }
|
||
|
Bird.prototype = Object.create(Animal.prototype);
|
||
|
let duck = new Bird();
|
||
|
duck.constructor // function Animal(){...}
|
||
|
```
|
||
|
|
||
|
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:
|
||
|
|
||
|
```js
|
||
|
Bird.prototype.constructor = Bird;
|
||
|
duck.constructor // function Bird(){...}
|
||
|
```
|
||
|
|
||
|
# --instructions--
|
||
|
|
||
|
Fix the code so `duck.constructor` and `beagle.constructor` return their respective constructors.
|
||
|
|
||
|
# --hints--
|
||
|
|
||
|
`Bird.prototype` should be an instance of `Animal`.
|
||
|
|
||
|
```js
|
||
|
assert(Animal.prototype.isPrototypeOf(Bird.prototype));
|
||
|
```
|
||
|
|
||
|
`duck.constructor` should return `Bird`.
|
||
|
|
||
|
```js
|
||
|
assert(duck.constructor === Bird);
|
||
|
```
|
||
|
|
||
|
`Dog.prototype` should be an instance of `Animal`.
|
||
|
|
||
|
```js
|
||
|
assert(Animal.prototype.isPrototypeOf(Dog.prototype));
|
||
|
```
|
||
|
|
||
|
`beagle.constructor` should return `Dog`.
|
||
|
|
||
|
```js
|
||
|
assert(beagle.constructor === Dog);
|
||
|
```
|
||
|
|
||
|
# --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();
|
||
|
```
|
||
|
|
||
|
# --solutions--
|
||
|
|
||
|
```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();
|
||
|
```
|