* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
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--
不应使用function
关键字定义方法。
(getUserInput) => assert(!removeJSComments(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);