* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
87 lines
1.6 KiB
Markdown
87 lines
1.6 KiB
Markdown
---
|
|
id: 587d7b8b367417b2b2512b50
|
|
title: Write Concise Declarative Functions with ES6
|
|
challengeType: 1
|
|
forumTopicId: 301224
|
|
dashedName: write-concise-declarative-functions-with-es6
|
|
---
|
|
|
|
# --description--
|
|
|
|
When defining functions within objects in ES5, we have to use the keyword `function` as follows:
|
|
|
|
```js
|
|
const person = {
|
|
name: "Taylor",
|
|
sayHello: function() {
|
|
return `Hello! My name is ${this.name}.`;
|
|
}
|
|
};
|
|
```
|
|
|
|
With ES6, You can remove the `function` keyword and colon altogether when defining functions in objects. Here's an example of this syntax:
|
|
|
|
```js
|
|
const person = {
|
|
name: "Taylor",
|
|
sayHello() {
|
|
return `Hello! My name is ${this.name}.`;
|
|
}
|
|
};
|
|
```
|
|
|
|
# --instructions--
|
|
|
|
Refactor the function `setGear` inside the object `bicycle` to use the shorthand syntax described above.
|
|
|
|
# --hints--
|
|
|
|
Traditional function expression should not be used.
|
|
|
|
```js
|
|
(getUserInput) => assert(!__helpers.removeJSComments(code).match(/function/));
|
|
```
|
|
|
|
`setGear` should be a declarative function.
|
|
|
|
```js
|
|
assert(
|
|
typeof bicycle.setGear === 'function' && code.match(/setGear\s*\(.+\)\s*\{/)
|
|
);
|
|
```
|
|
|
|
`bicycle.setGear(48)` should change the `gear` value to 48.
|
|
|
|
```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);
|
|
```
|