* chore(i18n,curriculum): update translations * chore: Italian to italian Co-authored-by: Nicholas Carrigan <nhcarrigan@gmail.com>
1.9 KiB
1.9 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 587d7dad367417b2b2512b77 | Definire una funzione costruttore | 1 | 16804 | define-a-constructor-function |
--description--
I costruttori sono funzioni che creano nuovi oggetti. Essi definiscono proprietà e comportamenti che appartengono al nuovo oggetto. Pensa ad essi come a progetti per la creazione di nuovi oggetti.
Ecco un esempio di costruttore:
function Bird() {
this.name = "Albert";
this.color = "blue";
this.numLegs = 2;
}
Questo costruttore definisce un oggetto Bird con proprietà name, color, e numLegs impostati rispettivamente su Albert, blue e 2. I costruttori seguono alcune convenzioni:
- I costruttori sono definiti con un nome con l'iniziale maiuscola per distinguerli dalle altre funzioni che non sono costruttori (
constructors). - I costruttori usano la parola chiave
thisper impostare le proprietà dell'oggetto che creeranno. All'interno del costruttore,thissi riferisce al nuovo oggetto che creerà. - I costruttori definiscono proprietà e comportamenti invece di restituire un valore come fanno le normali funzioni.
--instructions--
Crea un costruttore, Dog, con le proprietà name, color, e numLegs impostate rispettivamente su una stringa, una stringa, e un numero.
--hints--
Dog dovrebbe avere una proprietà name impostata su una stringa.
assert(typeof new Dog().name === 'string');
Dog dovrebbe avere una proprietà color impostata su una stringa.
assert(typeof new Dog().color === 'string');
Dog dovrebbe avere una proprietà numLegs impostata su un numero.
assert(typeof new Dog().numLegs === 'number');
--seed--
--seed-contents--
--solutions--
function Dog (name, color, numLegs) {
this.name = 'name';
this.color = 'color';
this.numLegs = 4;
}