mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-13 04:00:12 -04:00
1.7 KiB
1.7 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 587d7b8b367417b2b2512b50 | Scrivere funzioni dichiarative concise con ES6 | 1 | 301224 | write-concise-declarative-functions-with-es6 |
--description--
Quando si definiscono le funzioni all'interno degli oggetti in ES5, occorre usare la parola chiave function come segue:
const person = {
name: "Taylor",
sayHello: function() {
return `Hello! My name is ${this.name}.`;
}
};
Con ES6, è possibile rimuovere simultaneamente la parola chiave function e i due punti quando si definiscono le funzioni negli oggetti. Ecco un esempio di questa sintassi:
const person = {
name: "Taylor",
sayHello() {
return `Hello! My name is ${this.name}.`;
}
};
--instructions--
Riscrivi la funzione setGear all'interno dell'oggetto bicycle usando la scorciatoia sintattica descritta sopra.
--hints--
La dichiarazione di funzione tradizionale non deve essere utilizzata.
assert(!__helpers.removeJSComments(code).match(/function/));
setGear dovrebbe essere una funzione dichiarativa.
assert(
typeof bicycle.setGear === 'function' && __helpers.removeJSComments(code).match(/setGear\s*\(.+\)\s*\{/)
);
bicycle.setGear(48) dovrebbe cambiare il valore della marcia (gear) a 48.
bicycle.setGear(48);
assert(bicycle.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: function(newGear) {
setGear(newGear) {
this.gear = newGear;
}
};
bicycle.setGear(3);