2.2 KiB
id | title | challengeType | videoUrl | forumTopicId | dashedName |
---|---|---|---|---|---|
56533eb9ac21ba0edf2244c7 | Accedere alle proprietà dell'oggetto con la notazione a punti | 1 | https://scrimba.com/c/cGryJs8 | 16164 | accessing-object-properties-with-dot-notation |
--description--
Esistono due modi per accedere alle proprietà di un oggetto: notazione a punti (.
) e notazione a parentesi ([]
), simile a un array.
La notazione a punti è quella che usi quando conosci già il nome della proprietà a cui stai tentando di accedere.
Di seguito è riportato un esempio di utilizzo della notazione a punti (.
) per leggere la proprietà di un oggetto:
var myObj = {
prop1: "val1",
prop2: "val2"
};
var prop1val = myObj.prop1;
var prop2val = myObj.prop2;
prop1val
ha il valore della stringa val1
e prop2val
ha il avere un valore della stringa val2
.
--instructions--
Leggi i valori delle proprietà di testObj
utilizzando la notazione a punti. Imposta la variabile hatValue
in modo che sia uguale alla proprietà hat
dell'oggetto, e imposta la variabile shirtValue
in modo che sia uguale alla proprietà shirt
dell'oggetto.
--hints--
hatValue
dovrebbe essere una stringa
assert(typeof hatValue === 'string');
Il valore di hatValue
dovrebbe essere la stringa ballcap
assert(hatValue === 'ballcap');
shirtValue
dovrebbe essere una stringa
assert(typeof shirtValue === 'string');
Il valore di shirtValue
dovrebbe essere la stringa jersey
assert(shirtValue === 'jersey');
Dovresti usare due volte la notazione a punti
assert(code.match(/testObj\.\w+/g).length > 1);
--seed--
--after-user-code--
(function(a,b) { return "hatValue = '" + a + "', shirtValue = '" + b + "'"; })(hatValue,shirtValue);
--seed-contents--
// Setup
var testObj = {
"hat": "ballcap",
"shirt": "jersey",
"shoes": "cleats"
};
// Only change code below this line
var hatValue = testObj; // Change this line
var shirtValue = testObj; // Change this line
--solutions--
var testObj = {
"hat": "ballcap",
"shirt": "jersey",
"shoes": "cleats"
};
var hatValue = testObj.hat;
var shirtValue = testObj.shirt;