--- id: 587d7b8b367417b2b2512b50 title: Write Concise Declarative Functions with ES6 challengeType: 1 forumTopicId: 301224 localeTitle: Написание кратких декларативных функций с ES6 --- ## Description
При определении функций внутри объектов в ES5 мы должны использовать function ключевого слова следующим образом:
const person = {
имя: «Тейлор»,
sayHello: function () {
return `Hello! Меня зовут $ {this.name} .`;
}
};
С ES6 вы можете полностью удалить ключевое слово function и двоеточие при определении функций в объектах. Вот пример этого синтаксиса:
const person = {
имя: «Тейлор»,
скажи привет() {
return `Hello! Меня зовут $ {this.name} .`;
}
};
## Instructions
setGear функцию setGear внутри bicycle объекта, чтобы использовать сокращенный синтаксис, описанный выше.
## Tests
```yml tests: - text: Traditional function expression should not be used. testString: getUserInput => assert(!removeJSComments(code).match(/function/)); - text: setGear should be a declarative function. testString: assert(typeof bicycle.setGear === 'function' && code.match(/setGear\s*\(.+\)\s*\{/)); - text: bicycle.setGear(48) should change the gear value to 48. testString: assert((new bicycle.setGear(48)).gear === 48); ```
## Challenge Seed
```js // change code below this line const bicycle = { gear: 2, setGear: function(newGear) { this.gear = newGear; } }; // change code above this line bicycle.setGear(3); console.log(bicycle.gear); ```
### After Tests
```js const removeJSComments = str => str.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, ''); ```
## Solution
```js const bicycle = { gear: 2, setGear(newGear) { this.gear = newGear; } }; bicycle.setGear(3); ```