2018-09-30 23:01:58 +01:00
---
id: 587d7daf367417b2b2512b7e
title: Understand the Constructor Property
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301327
2021-01-13 03:31:00 +01:00
dashedName: understand-the-constructor-property
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
There is a special `constructor` property located on the object instances `duck` and `beagle` that were created in the previous challenges:
2019-05-17 06:20:30 -07:00
```js
let duck = new Bird();
let beagle = new Dog();
console.log(duck.constructor === Bird); //prints true
console.log(beagle.constructor === Dog); //prints true
```
2020-11-27 19:02:05 +01:00
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:
2019-05-17 06:20:30 -07:00
```js
function joinBirdFraternity(candidate) {
if (candidate.constructor === Bird) {
return true;
} else {
return false;
}
}
```
2020-11-27 19:02:05 +01:00
**Note**
Since the `constructor` property can be overwritten (which will be covered in the next two challenges) it’ s generally better to use the `instanceof` method to check the type of an object.
# --instructions--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
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` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`joinDogFraternity` should be defined as a function.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(typeof joinDogFraternity === 'function');
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`joinDogFraternity` should return true if`candidate` is an instance of `Dog` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(joinDogFraternity(new Dog('')) === true);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`joinDogFraternity` should use the `constructor` property.
```js
assert(/\.constructor/.test(code) && !/instanceof/.test(code));
```
# --seed--
## --seed-contents--
2018-09-30 23:01:58 +01:00
```js
function Dog(name) {
this.name = name;
}
2020-03-08 07:46:28 -07:00
// Only change code below this line
2018-09-30 23:01:58 +01:00
function joinDogFraternity(candidate) {
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
}
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
function Dog(name) {
this.name = name;
}
function joinDogFraternity(candidate) {
return candidate.constructor === Dog;
}
```