Files
freeCodeCamp/curriculum/challenges/italian/02-javascript-algorithms-and-data-structures/es6/write-concise-declarative-functions-with-es6.md
camperbot b3af21d50f chore(i18n,curriculum): update translations (#42487)
* chore(i18n,curriculum): update translations

* chore: Italian to italian

Co-authored-by: Nicholas Carrigan <nhcarrigan@gmail.com>
2021-06-14 11:34:20 -07:00

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, dobbiamo 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.

(getUserInput) => assert(!code.match(/function/));

setGear dovrebbe essere una funzione dichiarativa.

assert(
  typeof bicycle.setGear === 'function' && code.match(/setGear\s*\(.+\)\s*\{/)
);

bicycle.setGear(48) dovrebbe cambiare il valore della marcia (gear) a 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);