* chore(i18n,curriculum): update translations * chore: Italian to italian Co-authored-by: Nicholas Carrigan <nhcarrigan@gmail.com>
1.9 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7dae367417b2b2512b7a | Verificare il costruttore di un oggetto con instanceof | 1 | 301337 | verify-an-objects-constructor-with-instanceof |
--description--
Ogni volta che una funzione costruttore crea un nuovo oggetto, si dice che l'oggetto è un'istanza del suo costruttore. JavaScript offre un modo conveniente per verificarlo con l'operatore instanceof
. instanceof
ti permette di confrontare un oggetto con un costruttore, restituendo true
o false
in base al fatto che l'oggetto sia stato creato o meno con quel costruttore. Ecco un esempio:
let Bird = function(name, color) {
this.name = name;
this.color = color;
this.numLegs = 2;
}
let crow = new Bird("Alexis", "black");
crow instanceof Bird;
Questo metodo instanceof
restituirà true
.
Se un oggetto viene creato senza usare un costruttore, instanceof
verificherà che non si tratta di un'istanza di quel costruttore:
let canary = {
name: "Mildred",
color: "Yellow",
numLegs: 2
};
canary instanceof Bird;
Questo metodo instanceof
restituirà false
.
--instructions--
Crea una nuova istanza del costruttore House
, chiamandola myHouse
e passando un numero di camere da letto (bedrooms). Quindi, usa instanceof
per verificare che si tratti di un'istanza di House
.
--hints--
myHouse
dovrebbe avere un attributo numBedrooms
impostato su un numero.
assert(typeof myHouse.numBedrooms === 'number');
Dovresti verificare che myHouse
sia un'istanza di House
usando l'operatore instanceof
.
assert(/myHouse\s*instanceof\s*House/.test(code));
--seed--
--seed-contents--
function House(numBedrooms) {
this.numBedrooms = numBedrooms;
}
// Only change code below this line
--solutions--
function House(numBedrooms) {
this.numBedrooms = numBedrooms;
}
const myHouse = new House(4);
console.log(myHouse instanceof House);