1.5 KiB
1.5 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);