freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-an.../object-oriented-programming/understand-the-constructor-...

85 lines
2.0 KiB
Markdown
Raw Normal View History

---
id: 587d7daf367417b2b2512b7e
title: Understand the Constructor Property
challengeType: 1
forumTopicId: 301327
dashedName: understand-the-constructor-property
---
# --description--
There is a special `constructor` property located on the object instances `duck` and `beagle` that were created in the previous challenges:
```js
let duck = new Bird();
let beagle = new Dog();
console.log(duck.constructor === Bird); //prints true
console.log(beagle.constructor === Dog); //prints true
```
Note that the `constructor` property is a reference to the constructor function that created the instance. The advantage of the `constructor` property is that it's possible to check for this property to find out what kind of object it is. Here's an example of how this could be used:
```js
function joinBirdFraternity(candidate) {
if (candidate.constructor === Bird) {
return true;
} else {
return false;
}
}
```
**Note**
Since the `constructor` property can be overwritten (which will be covered in the next two challenges) its generally better to use the `instanceof` method to check the type of an object.
# --instructions--
Write a `joinDogFraternity` function that takes a `candidate` parameter and, using the `constructor` property, return `true` if the candidate is a `Dog`, otherwise return `false`.
# --hints--
`joinDogFraternity` should be defined as a function.
```js
assert(typeof joinDogFraternity === 'function');
```
`joinDogFraternity` should return true if`candidate` is an instance of `Dog`.
```js
assert(joinDogFraternity(new Dog('')) === true);
```
`joinDogFraternity` should use the `constructor` property.
```js
assert(/\.constructor/.test(code) && !/instanceof/.test(code));
```
# --seed--
## --seed-contents--
```js
function Dog(name) {
this.name = name;
}
// Only change code below this line
function joinDogFraternity(candidate) {
}
```
# --solutions--
```js
function Dog(name) {
this.name = name;
}
function joinDogFraternity(candidate) {
return candidate.constructor === Dog;
}
```