2.0 KiB
2.0 KiB
id, title, challengeType, forumTopicId, localeTitle
id | title | challengeType | forumTopicId | localeTitle |
---|---|---|---|---|
587d7b8b367417b2b2512b50 | Write Concise Declarative Functions with ES6 | 1 | 301224 | 用 ES6 编写简洁的函数声明 |
Description
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
函数。
Tests
tests:
- text: 不应使用<code>function</code>关键字定义方法。
testString: getUserInput => assert(!removeJSComments(code).match(/function/));
- text: <code>setGear</code>应是一个函数。
testString: assert(typeof bicycle.setGear === 'function' && code.match(/setGear\s*\(.+\)\s*\{/));
- text: 执行<code>bicycle.setGear(48)</code>应可以让<code>gear</code>的值变为 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 Test
const removeJSComments = str => str.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, '');
Solution
const bicycle = {
gear: 2,
setGear(newGear) {
this.gear = newGear;
}
};
bicycle.setGear(3);