* Use dfn tags * remove misused <dfn> tags * Revert "remove misused <dfn> tags" This reverts commit b24968a96810f618d831410ac90a0bc452ebde50. * Update curriculum/challenges/english/01-responsive-web-design/basic-html-and-html5/fill-in-the-blank-with-placeholder-text.english.md Co-Authored-By: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * Make "array" lowercase Co-Authored-By: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * Fix dfn usage * Address last dfn tags
2.1 KiB
2.1 KiB
id, title, challengeType, forumTopicId
id | title | challengeType | forumTopicId |
---|---|---|---|
587d7dae367417b2b2512b7a | Verify an Object's Constructor with instanceof | 1 | 301337 |
Description
instanceof
operator. instanceof
allows you to compare an object to a constructor, returning true
or false
based on whether or not that object was created with the constructor. Here's an example:
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, instanceof
will verify that it is not an instance of that constructor:
let canary = {
name: "Mildred",
color: "Yellow",
numLegs: 2
};
canary instanceof Bird; // => false
Instructions
House
constructor, calling it myHouse
and passing a number of bedrooms. Then, use instanceof
to verify that it is an instance of House
.
Tests
tests:
- text: <code>myHouse</code> should have a <code>numBedrooms</code> attribute set to a number.
testString: assert(typeof myHouse.numBedrooms === 'number');
- text: Be sure to 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));
Challenge Seed
/* jshint expr: true */
function House(numBedrooms) {
this.numBedrooms = numBedrooms;
}
// Add your code below this line
Solution
function House(numBedrooms) {
this.numBedrooms = numBedrooms;
}
const myHouse = new House(4);
console.log(myHouse instanceof House);