| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | --- | 
					
						
							|  |  |  | id: 587d7b8b367417b2b2512b50 | 
					
						
							| 
									
										
										
										
											2021-03-24 08:37:00 -06:00
										 |  |  | title: Escribe funciones breves y declarativas con ES6 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | challengeType: 1 | 
					
						
							|  |  |  | forumTopicId: 301224 | 
					
						
							|  |  |  | dashedName: write-concise-declarative-functions-with-es6 | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # --description--
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-20 00:31:24 -06:00
										 |  |  | Al definir funciones dentro de objetos en ES5, tenemos que utilizar la palabra clave `function` de la siguiente manera: | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | const person = { | 
					
						
							|  |  |  |   name: "Taylor", | 
					
						
							|  |  |  |   sayHello: function() { | 
					
						
							|  |  |  |     return `Hello! My name is ${this.name}.`; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-20 00:31:24 -06:00
										 |  |  | Con ES6, puedes eliminar la palabra clave `function` y los dos puntos al definir funciones en objetos. Aquí hay un ejemplo de esta sintaxis: | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | const person = { | 
					
						
							|  |  |  |   name: "Taylor", | 
					
						
							|  |  |  |   sayHello() { | 
					
						
							|  |  |  |     return `Hello! My name is ${this.name}.`; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # --instructions--
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-20 00:31:24 -06:00
										 |  |  | Refactoriza la función `setGear` dentro del objeto `bicycle` para que utilice la sintaxis abreviada descrita arriba. | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # --hints--
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-20 00:31:24 -06:00
										 |  |  | La expresión tradicional "function" no debe ser utilizada. | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							| 
									
										
										
										
											2021-04-28 16:18:54 +01:00
										 |  |  | (getUserInput) => assert(!code.match(/function/)); | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-20 00:31:24 -06:00
										 |  |  | `setGear` debe ser una función declarativa. | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert( | 
					
						
							|  |  |  |   typeof bicycle.setGear === 'function' && code.match(/setGear\s*\(.+\)\s*\{/) | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-20 00:31:24 -06:00
										 |  |  | `bicycle.setGear(48)` debe cambiar el valor de `gear` a 48. | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert(new bicycle.setGear(48).gear === 48); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # --seed--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## --seed-contents--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | // Only change code below this line | 
					
						
							|  |  |  | const bicycle = { | 
					
						
							|  |  |  |   gear: 2, | 
					
						
							|  |  |  |   setGear: function(newGear) { | 
					
						
							|  |  |  |     this.gear = newGear; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | // Only change code above this line | 
					
						
							|  |  |  | bicycle.setGear(3); | 
					
						
							|  |  |  | console.log(bicycle.gear); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # --solutions--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | const bicycle = { | 
					
						
							|  |  |  |   gear: 2, | 
					
						
							|  |  |  |   setGear(newGear) { | 
					
						
							|  |  |  |     this.gear = newGear; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | bicycle.setGear(3); | 
					
						
							|  |  |  | ``` |