Files
.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
debugging
es6
functional-programming
intermediate-algorithm-scripting
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-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
portuguese
russian
spanish
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
index.js
lib.js
md-translation.js
package-entry.js
package-lock.json
package.json
utils.js
docs
search-indexing
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.prettierignore
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
lerna.json
package-lock.json
package.json
sample.env
freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/verify-an-objects-constructor-with-instanceof.english.md

92 lines
2.1 KiB
Markdown
Raw Normal View History

---
id: 587d7dae367417b2b2512b7a
title: Verify an Object's Constructor with instanceof
challengeType: 1
forumTopicId: 301337
---
## Description
<section id='description'>
Anytime a constructor function creates a new object, that object is said to be an <dfn>instance</dfn> of its constructor. JavaScript gives a convenient way to verify this with the <code>instanceof</code> operator. <code>instanceof</code> allows you to compare an object to a constructor, returning <code>true</code> or <code>false</code> based on whether or not that object was created with the constructor. Here's an example:
```js
let Bird = function(name, color) {
this.name = name;
this.color = color;
this.numLegs = 2;
}
let crow = new Bird("Alexis", "black");
crow instanceof Bird; // => true
```
If an object is created without using a constructor, <code>instanceof</code> will verify that it is not an instance of that constructor:
```js
let canary = {
name: "Mildred",
color: "Yellow",
numLegs: 2
};
canary instanceof Bird; // => false
```
</section>
## Instructions
<section id='instructions'>
Create a new instance of the <code>House</code> constructor, calling it <code>myHouse</code> and passing a number of bedrooms. Then, use <code>instanceof</code> to verify that it is an instance of <code>House</code>.
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: <code>myHouse</code> should have a <code>numBedrooms</code> attribute set to a number.
testString: assert(typeof myHouse.numBedrooms === 'number');
- text: You should verify that <code>myHouse</code> is an instance of <code>House</code> using the <code>instanceof</code> operator.
testString: assert(/myHouse\s*instanceof\s*House/.test(code));
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function House(numBedrooms) {
this.numBedrooms = numBedrooms;
}
// Only change code below this line
```
</div>
</section>
## Solution
<section id='solution'>
```js
function House(numBedrooms) {
this.numBedrooms = numBedrooms;
}
const myHouse = new House(4);
console.log(myHouse instanceof House);
```
</section>