freeCodeCamp/curriculum/challenges/russian/02-javascript-algorithms-an.../es6/write-concise-declarative-f...

2.4 KiB
Raw Blame History

id title challengeType forumTopicId localeTitle
587d7b8b367417b2b2512b50 Write Concise Declarative Functions with ES6 1 301224 Написание кратких декларативных функций с 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

tests:
  - text: Traditional function expression should not be used.
    testString: getUserInput => assert(!removeJSComments(code).match(/function/));
  - text: <code>setGear</code> should be a declarative function.
    testString: assert(typeof bicycle.setGear === 'function' && code.match(/setGear\s*\(.+\)\s*\{/));
  - text: <code>bicycle.setGear(48)</code> should change the <code>gear</code> value to 48.
    testString: assert((new bicycle.setGear(48)).gear === 48);

Challenge Seed

// 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

const removeJSComments = str => str.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, '');

Solution

const bicycle = {
  gear: 2,
  setGear(newGear) {
    this.gear = newGear;
  }
};
bicycle.setGear(3);