instance
of its constructor. JavaScript gives a convenient way to verify this with the 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) {If an object is created without using a constructor,
this.name = name;
this.color = color;
this.numLegs = 2;
}
let crow = new Bird("Alexis", "black");
crow instanceof Bird; // => true
instanceof
will verify that it is not an instance of that constructor:
let canary = {
name: "Mildred",
color: "Yellow",
numLegs: 2
};
canary instanceof Bird; // => false
House
constructor, calling it myHouse
and passing a number of bedrooms. Then, use instanceof
to verify that it is an instance of House
.
myHouse
should have a numBedrooms
attribute set to a number.
testString: 'assert(typeof myHouse.numBedrooms === ''number'', ''myHouse
should have a numBedrooms
attribute set to a number.'');'
- text: Be sure to verify that myHouse
is an instance of House
using the instanceof
operator.
testString: 'assert(/myHouse\s*instanceof\s*House/.test(code), ''Be sure to verify that myHouse
is an instance of House
using the instanceof
operator.'');'
```