curriculum/challenges/english/blocks/es6/587d7b8b367417b2b2512b50.md
When defining functions within objects in ES5, we have to use the keyword function as follows:
const person = {
name: "Taylor",
sayHello: function() {
return `Hello! My name is ${this.name}.`;
}
};
With ES6, you can remove the function keyword and colon altogether when defining functions in objects. Here's an example of this syntax:
const person = {
name: "Taylor",
sayHello() {
return `Hello! My name is ${this.name}.`;
}
};
Refactor the function setGear inside the object bicycle to use the shorthand syntax described above.
Traditional function expression should not be used.
assert(!__helpers.removeJSComments(code).match(/function/));
setGear should be a declarative function.
assert(
typeof bicycle.setGear === 'function' && __helpers.removeJSComments(code).match(/setGear\s*\(.+\)\s*\{/)
);
bicycle.setGear(48) should change the gear value to 48.
bicycle.setGear(48);
assert(bicycle.gear === 48);
// 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);
const bicycle = {
gear: 2,
// setGear: function(newGear) {
setGear(newGear) {
this.gear = newGear;
}
};
bicycle.setGear(3);