| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | --- | 
					
						
							|  |  |  |  | id: 587d7daf367417b2b2512b7d | 
					
						
							| 
									
										
										
										
											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: 301320 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  |  | dashedName: iterate-over-all-properties | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | --- | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | # --description--
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-31 09:47:25 -07:00
										 |  |  |  | 现在你已经了解了两种属性: <dfn>自身属性</dfn>和 `prototype` 属性。 自身属性是直接在对象上定义的。 而原型属性在 `prototype` 上定义。 | 
					
						
							| 
									
										
										
										
											2020-08-04 15:15:28 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | ```js | 
					
						
							|  |  |  |  | function Bird(name) { | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  |   this.name = name;  //own property | 
					
						
							| 
									
										
										
										
											2020-08-04 15:15:28 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  | Bird.prototype.numLegs = 2; // prototype property | 
					
						
							| 
									
										
										
										
											2020-08-04 15:15:28 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | let duck = new Bird("Donald"); | 
					
						
							|  |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 00:04:04 +09:00
										 |  |  |  | 这个示例会告诉你如何将 `duck` 的自身属性和 `prototype` 属性分别添加到 `ownProps` 数组和 `prototypeProps` 数组里面: | 
					
						
							| 
									
										
										
										
											2020-08-04 15:15:28 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | ```js | 
					
						
							|  |  |  |  | let ownProps = []; | 
					
						
							|  |  |  |  | let prototypeProps = []; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | for (let property in duck) { | 
					
						
							|  |  |  |  |   if(duck.hasOwnProperty(property)) { | 
					
						
							|  |  |  |  |     ownProps.push(property); | 
					
						
							|  |  |  |  |   } else { | 
					
						
							|  |  |  |  |     prototypeProps.push(property); | 
					
						
							|  |  |  |  |   } | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  |  | console.log(ownProps); | 
					
						
							|  |  |  |  | console.log(prototypeProps); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:15:28 +08:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  |  | `console.log(ownProps)` 将在控制台中显示 `["name"]` ,`console.log(prototypeProps)` 将显示 `["numLegs"]`。 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | # --instructions--
 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 00:04:04 +09:00
										 |  |  |  | 将 `beagle` 的自身属性都添加到 `ownProps` 数组里面去。 将 `Dog` 中所有的 `prototype` 属性都添加到 `prototypeProps` 数组中。 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |  | `ownProps` 数组应该包含 `name`。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | ```js | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  | assert.deepEqual(ownProps, ['name']); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  |  | `prototypeProps` 数组应该包含 `numLegs`。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | ```js | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  | assert.deepEqual(prototypeProps, ['numLegs']); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  |  | 在不使用内置方法 `Object.keys()` 的前提下完成这个挑战。 | 
					
						
							| 
									
										
										
										
											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(!/\Object.keys/.test(code)); | 
					
						
							| 
									
										
										
										
											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 Dog(name) { | 
					
						
							|  |  |  |  |   this.name = name; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | Dog.prototype.numLegs = 4; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | let beagle = new Dog("Snoopy"); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | let ownProps = []; | 
					
						
							|  |  |  |  | let prototypeProps = []; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // Only change code below this line | 
					
						
							|  |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | # --solutions--
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  |  | ```js | 
					
						
							|  |  |  |  | function Dog(name) { | 
					
						
							|  |  |  |  |   this.name = name; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | Dog.prototype.numLegs = 4; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | let beagle = new Dog("Snoopy"); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | let ownProps = []; | 
					
						
							|  |  |  |  | let prototypeProps = []; | 
					
						
							|  |  |  |  | for (let prop in beagle) { | 
					
						
							|  |  |  |  |   if (beagle.hasOwnProperty(prop)) { | 
					
						
							|  |  |  |  |     ownProps.push(prop); | 
					
						
							|  |  |  |  |   } else { | 
					
						
							|  |  |  |  |     prototypeProps.push(prop); | 
					
						
							|  |  |  |  |   } | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | ``` |