1.6 KiB
1.6 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7b8b367417b2b2512b50 | Escrever funções declarativas concisas com ES6 | 1 | 301224 | write-concise-declarative-functions-with-es6 |
--description--
Ao definir funções dentro de objetos em ES5, nós temos de usar a palavra-chave function
como se segue:
const person = {
name: "Taylor",
sayHello: function() {
return `Hello! My name is ${this.name}.`;
}
};
Com ES6, você pode remover a palavra-chave function
e dois pontos ao definir funções em objetos. Aqui está um exemplo dessa sintaxe:
const person = {
name: "Taylor",
sayHello() {
return `Hello! My name is ${this.name}.`;
}
};
--instructions--
Refatore a função setGear
dentro do objeto bicycle
para usar a sintaxe curta descrita acima.
--hints--
Expressão tradicional de função não deve ser usado.
(getUserInput) => assert(!code.match(/function/));
setGear
deve ser uma função declarativa.
assert(
typeof bicycle.setGear === 'function' && code.match(/setGear\s*\(.+\)\s*\{/)
);
bicycle.setGear(48)
deve alterar o valor de gear
para 48.
assert(new bicycle.setGear(48).gear === 48);
--seed--
--seed-contents--
// 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--
const bicycle = {
gear: 2,
setGear(newGear) {
this.gear = newGear;
}
};
bicycle.setGear(3);