freeCodeCamp/curriculum/challenges/italian/02-javascript-algorithms-an.../basic-javascript/accessing-object-properties...

100 lines
2.2 KiB
Markdown

---
id: 56533eb9ac21ba0edf2244c7
title: Accedere alle proprietà dell'oggetto con la notazione a punti
challengeType: 1
videoUrl: 'https://scrimba.com/c/cGryJs8'
forumTopicId: 16164
dashedName: 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:
```js
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
```js
assert(typeof hatValue === 'string');
```
Il valore di `hatValue` dovrebbe essere la stringa `ballcap`
```js
assert(hatValue === 'ballcap');
```
`shirtValue` dovrebbe essere una stringa
```js
assert(typeof shirtValue === 'string');
```
Il valore di `shirtValue` dovrebbe essere la stringa `jersey`
```js
assert(shirtValue === 'jersey');
```
Dovresti usare due volte la notazione a punti
```js
assert(code.match(/testObj\.\w+/g).length > 1);
```
# --seed--
## --after-user-code--
```js
(function(a,b) { return "hatValue = '" + a + "', shirtValue = '" + b + "'"; })(hatValue,shirtValue);
```
## --seed-contents--
```js
// 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--
```js
var testObj = {
"hat": "ballcap",
"shirt": "jersey",
"shoes": "cleats"
};
var hatValue = testObj.hat;
var shirtValue = testObj.shirt;
```