2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: 587d7daf367417b2b2512b7e
|
2021-03-15 03:20:39 +00:00
|
|
|
title: 了解构造函数属性
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 1
|
2020-08-04 07:15:28 +00:00
|
|
|
forumTopicId: 301327
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: understand-the-constructor-property
|
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
|
|
|
在上一个挑战中创建的实例对象 `duck` 和 `beagle` 都有一个特殊的 `constructor` 属性:
|
2020-08-04 07:15:28 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
let duck = new Bird();
|
|
|
|
let beagle = new Dog();
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
console.log(duck.constructor === Bird);
|
|
|
|
console.log(beagle.constructor === Dog);
|
2020-08-04 07:15:28 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
这两次 `console.log` 调用都将在控制台中显示 `true`。
|
|
|
|
|
|
|
|
需要注意到的是这个 `constructor` 属性是对创建这个实例的构造函数的一个引用。 `constructor` 属性的一个好处是可以通过检查这个属性来找出它是一个什么对象。 下面是一个例子,来看看是怎么使用的:
|
2020-08-04 07:15:28 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
function joinBirdFraternity(candidate) {
|
|
|
|
if (candidate.constructor === Bird) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
**注意:** 由于 `constructor` 属性可以被重写(在下面两节挑战中将会遇到),所以最好使用`instanceof` 方法来检查对象的类型。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
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
|
|
|
写一个 `joinDogFraternity` 函数,传入一个 `candidate` 参数并使用 `constructor` 属性来判断传入的 candidate 是不是 `Dog` 创建的对象实例,如果是,就返回 `true`,否则返回 `false`。
|
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
|
|
|
`joinDogFraternity` 应该被定义为一个函数。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(typeof joinDogFraternity === 'function');
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
如果 `candidate` 是 `Dog` 的一个对象实例,则 `joinDogFraternity` 函数应该返回 `true`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(joinDogFraternity(new Dog('')) === true);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`joinDogFraternity` 中应该用到 `constructor` 属性。
|
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(/\.constructor/.test(code) && !/instanceof/.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 Dog(name) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only change code below this line
|
|
|
|
function joinDogFraternity(candidate) {
|
|
|
|
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```js
|
|
|
|
function Dog(name) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
function joinDogFraternity(candidate) {
|
|
|
|
return candidate.constructor === Dog;
|
|
|
|
}
|
|
|
|
```
|