2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: 587d7dae367417b2b2512b7a
|
2021-03-15 03:20:39 +00:00
|
|
|
title: 使用 instanceof 验证对象的构造函数
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 1
|
2020-08-04 07:15:28 +00:00
|
|
|
forumTopicId: 301337
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: verify-an-objects-constructor-with-instanceof
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
凡是通过构造函数创建出的新对象,这个对象都叫做这个构造函数的 <dfn>instance</dfn>。 JavaScript 提供了一种很简便的方法来验证这个事实,那就是通过 `instanceof` 操作符。 `instanceof` 允许你将对象与构造函数之间进行比较,根据对象是否由这个构造函数创建的返回 `true` 或者 `false`。 以下是一个示例:
|
2020-08-04 07:15:28 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
let Bird = function(name, color) {
|
|
|
|
this.name = name;
|
|
|
|
this.color = color;
|
|
|
|
this.numLegs = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
let crow = new Bird("Alexis", "black");
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
crow instanceof Bird;
|
2020-08-04 07:15:28 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`instanceof` 方法会返回 `true`.
|
|
|
|
|
|
|
|
如果一个对象不是使用构造函数创建的,那么 `instanceof` 将会验证这个对象不是构造函数的实例:
|
2020-08-04 07:15:28 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
let canary = {
|
|
|
|
name: "Mildred",
|
|
|
|
color: "Yellow",
|
|
|
|
numLegs: 2
|
|
|
|
};
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
canary instanceof Bird;
|
2020-08-04 07:15:28 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`instanceof` 方法会返回 `false`。
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
给 `House` 构造函数创建一个新实例,取名为 `myHouse` 并且传递一个数字给 bedrooms 参数。 然后使用 `instanceof` 操作符验证这个对象是否为 `House` 的实例。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`myHouse` 应该有一个 `numBedrooms` 属性值被赋为一个数字。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(typeof myHouse.numBedrooms === 'number');
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
应该使用 `instanceof` 操作符验证 `myHouse` 这个对象是 `House` 构造函数的一个实例。
|
2020-08-04 07:15:28 +00:00
|
|
|
|
2018-10-10 22:03:03 +00:00
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(/myHouse\s*instanceof\s*House/.test(code));
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
2020-08-04 07:15:28 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
function House(numBedrooms) {
|
|
|
|
this.numBedrooms = numBedrooms;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only change code below this line
|
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```js
|
|
|
|
function House(numBedrooms) {
|
|
|
|
this.numBedrooms = numBedrooms;
|
|
|
|
}
|
|
|
|
const myHouse = new House(4);
|
|
|
|
console.log(myHouse instanceof House);
|
|
|
|
```
|