| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | id: 587d7db1367417b2b2512b86 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | title: 重置一个继承的构造函数属性 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | challengeType: 1 | 
					
						
							| 
									
										
										
										
											2020-08-04 15:15:28 +08:00
										 |  |  | forumTopicId: 301324 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | dashedName: reset-an-inherited-constructor-property | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --description--
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | 当一个对象从另一个对象那里继承了其 `prototype` 时,那它也继承了父类的 constructor 属性。 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | 请看下面的举例: | 
					
						
							| 
									
										
										
										
											2020-08-04 15:15:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | function Bird() { } | 
					
						
							|  |  |  | Bird.prototype = Object.create(Animal.prototype); | 
					
						
							|  |  |  | let duck = new Bird(); | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | duck.constructor | 
					
						
							| 
									
										
										
										
											2020-08-04 15:15:28 +08:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-21 19:13:43 +05:30
										 |  |  | 但是 `duck` 和其他所有 `Bird` 的实例都应该表明它们是由 `Bird` 创建的,而不是由 `Animal` 创建的。 为此,你可以手动将 `Bird` 的构造函数属性设置为 `Bird` 对象: | 
					
						
							| 
									
										
										
										
											2020-08-04 15:15:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | Bird.prototype.constructor = Bird; | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | duck.constructor | 
					
						
							| 
									
										
										
										
											2020-08-04 15:15:28 +08:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --instructions--
 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | 修改你的代码,使得 `duck.constructor` 和 `beagle.constructor` 返回各自的构造函数。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --hints--
 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | `Bird.prototype` 应该是 `Animal` 的一个实例。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | ```js | 
					
						
							|  |  |  | assert(Animal.prototype.isPrototypeOf(Bird.prototype)); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | `duck.constructor` 应该返回 `Bird`。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | assert(duck.constructor === Bird); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | `Dog.prototype` 应该是 `Animal` 的一个实例。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | ```js | 
					
						
							|  |  |  | assert(Animal.prototype.isPrototypeOf(Dog.prototype)); | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | `beagle.constructor` 应该返回 `Dog`。 | 
					
						
							| 
									
										
										
										
											2020-08-04 15:15:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | ```js | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | assert(beagle.constructor === Dog); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-08-04 15:15:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | # --seed--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## --seed-contents--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | function Animal() { } | 
					
						
							|  |  |  | function Bird() { } | 
					
						
							|  |  |  | function Dog() { } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Bird.prototype = Object.create(Animal.prototype); | 
					
						
							|  |  |  | Dog.prototype = Object.create(Animal.prototype); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Only change code below this line | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let duck = new Bird(); | 
					
						
							|  |  |  | let beagle = new Dog(); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --solutions--
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | ```js | 
					
						
							|  |  |  | function Animal() { } | 
					
						
							|  |  |  | function Bird() { } | 
					
						
							|  |  |  | function Dog() { } | 
					
						
							|  |  |  | Bird.prototype = Object.create(Animal.prototype); | 
					
						
							|  |  |  | Dog.prototype = Object.create(Animal.prototype); | 
					
						
							|  |  |  | Dog.prototype.constructor = Dog; | 
					
						
							|  |  |  | Bird.prototype.constructor = Bird; | 
					
						
							|  |  |  | let duck = new Bird(); | 
					
						
							|  |  |  | let beagle = new Dog(); | 
					
						
							|  |  |  | ``` |