* fix: convert js algorithms and data structures * fix: revert some blocks back to blockquote * fix: reverted comparison code block to blockquotes * fix: change js to json Co-Authored-By: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> * fix: convert various section to triple backticks * fix: Make the formatting consistent for comparisons
2.5 KiB
2.5 KiB
id, title, challengeType
| id | title | challengeType |
|---|---|---|
| 587d7db1367417b2b2512b86 | Reset an Inherited Constructor Property | 1 |
Description
prototype from another object, it also inherits the supertype's constructor property.
Here's an example:
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:
Bird.prototype.constructor = Bird;
duck.constructor // function Bird(){...}
Instructions
duck.constructor and beagle.constructor return their respective constructors.
Tests
tests:
- text: <code>Bird.prototype</code> should be an instance of <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> should return <code>Bird</code>.
testString: assert(duck.constructor === Bird, '<code>duck.constructor</code> should return <code>Bird</code>.');
- text: <code>Dog.prototype</code> should be an instance of <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> should return <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();