Files
.github
api-server
client
config
curriculum
__fixtures__
challenges
_meta
arabic
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
basic-javascript-rpg-game
debugging
es6
functional-programming
functional-programming-spreadsheet
intermediate-algorithm-scripting
intermediate-javascript-calorie-counter
javascript-algorithms-and-data-structures-projects
object-oriented-programming
add-methods-after-inheritance.english.md
change-the-prototype-to-a-new-object.english.md
create-a-basic-javascript-object.english.md
create-a-method-on-an-object.english.md
define-a-constructor-function.english.md
extend-constructors-to-receive-arguments.english.md
inherit-behaviors-from-a-supertype.english.md
iterate-over-all-properties.english.md
make-code-more-reusable-with-the-this-keyword.english.md
override-inherited-methods.english.md
remember-to-set-the-constructor-property-when-changing-the-prototype.english.md
reset-an-inherited-constructor-property.english.md
set-the-childs-prototype-to-an-instance-of-the-parent.english.md
understand-own-properties.english.md
understand-the-constructor-property.english.md
understand-the-immediately-invoked-function-expression-iife.english.md
understand-the-prototype-chain.english.md
understand-where-an-objects-prototype-comes-from.english.md
use-a-constructor-to-create-objects.english.md
use-a-mixin-to-add-common-behavior-between-unrelated-objects.english.md
use-an-iife-to-create-a-module.english.md
use-closure-to-protect-properties-within-an-object-from-being-modified-externally.english.md
use-dot-notation-to-access-the-properties-of-an-object.english.md
use-inheritance-so-you-dont-repeat-yourself.english.md
use-prototype-properties-to-reduce-duplicate-code.english.md
verify-an-objects-constructor-with-instanceof.english.md
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-quality-assurance
07-scientific-computing-with-python
08-data-analysis-with-python
09-information-security
10-coding-interview-prep
11-machine-learning-with-python
12-certificates
portuguese
russian
spanish
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
comment-dictionary.js
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.acceptance.test.js
getChallenges.js
getChallenges.test.js
gulpfile.js
lib.js
package-entry.js
package-lock.json
package.json
utils.js
cypress
docs
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.npmrc
.prettierignore
.prettierrc
.snyk
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
crowdin.yml
cypress-install.js
cypress.json
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
jest.config.js
lerna.json
lighthouserc.js
package-lock.json
package.json
sample.env
freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/reset-an-inherited-constructor-property.english.md
Oliver Eyton-Williams bd68b70f3d Feat: hide blocks not challenges ()
* fix: remove isHidden flag from frontmatter

* fix: add isUpcomingChange

Co-authored-by: Ahmad Abdolsaheb <ahmad.abdolsaheb@gmail.com>

* feat: hide blocks not challenges

Co-authored-by: Ahmad Abdolsaheb <ahmad.abdolsaheb@gmail.com>

Co-authored-by: Ahmad Abdolsaheb <ahmad.abdolsaheb@gmail.com>
2020-09-03 15:07:40 -07:00

2.2 KiB

id, title, challengeType, forumTopicId
id title challengeType forumTopicId
587d7db1367417b2b2512b86 Reset an Inherited Constructor Property 1 301324

Description

When an object inherits its 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

Fix the code so 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));
  - text: <code>duck.constructor</code> should return <code>Bird</code>.
    testString: assert(duck.constructor === Bird);
  - text: <code>Dog.prototype</code> should be an instance of <code>Animal</code>.
    testString: assert(Animal.prototype.isPrototypeOf(Dog.prototype));
  - text: <code>beagle.constructor</code> should return <code>Dog</code>.
    testString: assert(beagle.constructor === Dog);

Challenge Seed

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();

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();