1.8 KiB
1.8 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7b8b367417b2b2512b50 | ES6 を使用して簡潔な宣言的関数を記述する | 1 | 301224 | write-concise-declarative-functions-with-es6 |
--description--
ES5 では、オブジェクト内で関数を定義する場合、次のようにキーワード function
を使用する必要があります。
const person = {
name: "Taylor",
sayHello: function() {
return `Hello! My name is ${this.name}.`;
}
};
ES6 では、オブジェクト内で関数を定義する場合、function
キーワードとコロンを完全に省略できます。 この構文の例を次に示します。
const person = {
name: "Taylor",
sayHello() {
return `Hello! My name is ${this.name}.`;
}
};
--instructions--
オブジェクト bicycle
内の関数 setGear
を、上記で説明した簡潔な構文を使用してリファクタリングしてください。
--hints--
従来の関数式は使用しないでください。
(getUserInput) => assert(!code.match(/function/));
setGear
は宣言的な関数にする必要があります。
assert(
typeof bicycle.setGear === 'function' && code.match(/setGear\s*\(.+\)\s*\{/)
);
bicycle.setGear(48)
は、gear
の値を 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);