| 
									
										
										
										
											2020-08-04 15:13:35 +08:00
										 |  |  | --- | 
					
						
							|  |  |  | id: 587d7b8a367417b2b2512b4f | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | title: 使用简单字段编写简洁的对象字面量声明 | 
					
						
							| 
									
										
										
										
											2020-08-04 15:13:35 +08:00
										 |  |  | challengeType: 1 | 
					
						
							|  |  |  | forumTopicId: 301225 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | dashedName: write-concise-object-literal-declarations-using-object-property-shorthand | 
					
						
							| 
									
										
										
										
											2020-08-04 15:13:35 +08:00
										 |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --description--
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | ES6 添加了一些很棒的功能,用于更方便地定义对象。 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | 请看以下代码: | 
					
						
							| 
									
										
										
										
											2020-08-04 15:13:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | const getMousePosition = (x, y) => ({ | 
					
						
							|  |  |  |   x: x, | 
					
						
							|  |  |  |   y: y | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | `getMousePosition` 简单的函数,返回拥有两个属性的对象。 ES6 提供了一个语法糖,消除了类似 `x: x` 这种冗余的写法。 你可以只写一次 `x`,解释器会自动将其转换成 `x: x`(或效果相同的内容)。 下面是使用这种语法重写的同样的函数: | 
					
						
							| 
									
										
										
										
											2020-08-04 15:13:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | const getMousePosition = (x, y) => ({ x, y }); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --instructions--
 | 
					
						
							| 
									
										
										
										
											2020-08-04 15:13:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | 请使用简单属性对象的语法来创建并返回一个具有 `name`、`age` 和 `gender` 属性的对象。 | 
					
						
							| 
									
										
										
										
											2020-08-04 15:13:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --hints--
 | 
					
						
							| 
									
										
										
										
											2020-08-04 15:13:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | `createPerson("Zodiac Hasbro", 56, "male")` 应该返回 `{name: "Zodiac Hasbro", age: 56, gender: "male"}`。 | 
					
						
							| 
									
										
										
										
											2020-08-04 15:13:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | assert.deepEqual( | 
					
						
							|  |  |  |   { name: 'Zodiac Hasbro', age: 56, gender: 'male' }, | 
					
						
							|  |  |  |   createPerson('Zodiac Hasbro', 56, 'male') | 
					
						
							|  |  |  | ); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:13:35 +08:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | 不要使用 `key:value`。 | 
					
						
							| 
									
										
										
										
											2020-08-04 15:13:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | (getUserInput) => assert(!getUserInput('index').match(/:/g)); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:13:35 +08:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | # --seed--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## --seed-contents--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | const createPerson = (name, age, gender) => { | 
					
						
							|  |  |  |   // Only change code below this line | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     name: name, | 
					
						
							|  |  |  |     age: age, | 
					
						
							|  |  |  |     gender: gender | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   // Only change code above this line | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --solutions--
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | ```js | 
					
						
							|  |  |  | const createPerson = (name, age, gender) => { | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     name, | 
					
						
							|  |  |  |     age, | 
					
						
							|  |  |  |     gender | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | ``` |