* chore(i18n,curriculum): update translations * chore: Italian to italian Co-authored-by: Nicholas Carrigan <nhcarrigan@gmail.com>
1.7 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7dad367417b2b2512b78 | Usare un costruttore per creare oggetti | 1 | 18233 | use-a-constructor-to-create-objects |
--description--
Ecco il costruttore di Bird
della sfida precedente:
function Bird() {
this.name = "Albert";
this.color = "blue";
this.numLegs = 2;
}
let blueBird = new Bird();
NOTA: this
all'interno del costruttore si riferisce sempre all'oggetto che viene creato.
Nota che l'operatore new
viene utilizzato per chiamare un costruttore. Questo dice a JavaScript di creare una nuova istanza di Bird
chiamata blueBird
. Senza l'operatore new
, this
all'interno del costruttore non punterebbe all'oggetto appena creato, dando risultati inattesi. Ora blueBird
ha tutte le proprietà definite all'interno del costruttore Bird
:
blueBird.name;
blueBird.color;
blueBird.numLegs;
Proprio come qualsiasi altro oggetto, le sue proprietà sono accessibili e possono essere modificate:
blueBird.name = 'Elvira';
blueBird.name;
--instructions--
Usa il costruttore Dog
dell'ultima lezione per creare una nuova istanza di Dog
, assegnandola a una variabile hound
.
--hints--
hound
dovrebbe essere creato usando il costruttore Dog
.
assert(hound instanceof Dog);
Il tuo codice dovrebbe utilizzare l'operatore new
per creare un'istanza di Dog
.
assert(code.match(/new/g));
--seed--
--seed-contents--
function Dog() {
this.name = "Rupert";
this.color = "brown";
this.numLegs = 4;
}
// Only change code below this line
--solutions--
function Dog() {
this.name = "Rupert";
this.color = "brown";
this.numLegs = 4;
}
const hound = new Dog();