2021-06-15 00:49:18 -07:00
---
id: 56533eb9ac21ba0edf2244c7
2021-07-21 20:53:20 +05:30
title: Acessar propriedades de objetos com notação de pontos
2021-06-15 00:49:18 -07:00
challengeType: 1
videoUrl: 'https://scrimba.com/c/cGryJs8'
forumTopicId: 16164
dashedName: accessing-object-properties-with-dot-notation
---
# --description--
2021-07-09 21:23:54 -07:00
Existem duas formas para acessar as propriedades de um objeto: notação de ponto (`.` ) e notação de colchetes (`[]` ), de forma similar a um array.
2021-06-15 00:49:18 -07:00
2021-07-09 21:23:54 -07:00
Notação de ponto é o que você utiliza quando você sabe o nome da propriedade que você está tentando acessar antecipadamente.
2021-06-15 00:49:18 -07:00
2021-07-09 21:23:54 -07:00
Aqui está um exemplo usando notação de ponto (`.` ) para ler uma propriedade do objeto:
2021-06-15 00:49:18 -07:00
```js
2021-10-27 15:10:57 +00:00
const myObj = {
2021-06-15 00:49:18 -07:00
prop1: "val1",
prop2: "val2"
};
2021-10-27 15:10:57 +00:00
const prop1val = myObj.prop1;
const prop2val = myObj.prop2;
2021-06-15 00:49:18 -07:00
```
2021-07-26 23:39:21 +09:00
`prop1val` teria o valor `val1` e `prop2val` teria o valor `val2` .
2021-10-27 15:10:57 +00:00
2021-06-15 00:49:18 -07:00
# --instructions--
2021-07-09 21:23:54 -07:00
Leia os valores de propriedade de `testObj` usando a notação de ponto. Defina a variável `hatValue` igual à propriedade `hat` do objeto e defina a variável `shirtValue` igual à propriedade `shirt` do objeto.
2021-06-15 00:49:18 -07:00
# --hints--
2021-07-09 21:23:54 -07:00
`hatValue` deve ser uma string
2021-06-15 00:49:18 -07:00
```js
assert(typeof hatValue === 'string');
```
2021-07-09 21:23:54 -07:00
O valor de `hatValue` deve ser a string `ballcap`
2021-06-15 00:49:18 -07:00
```js
assert(hatValue === 'ballcap');
```
2021-07-09 21:23:54 -07:00
`shirtValue` deve ser a string
2021-06-15 00:49:18 -07:00
```js
assert(typeof shirtValue === 'string');
```
2021-07-09 21:23:54 -07:00
O valor de `shirtValue` deve ser a string `jersey`
2021-06-15 00:49:18 -07:00
```js
assert(shirtValue === 'jersey');
```
2021-07-09 21:23:54 -07:00
Você deve usar notação de ponto duas vezes
2021-06-15 00:49:18 -07:00
```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
2021-10-27 15:10:57 +00:00
const testObj = {
2021-06-15 00:49:18 -07:00
"hat": "ballcap",
"shirt": "jersey",
"shoes": "cleats"
};
// Only change code below this line
2021-10-27 15:10:57 +00:00
const hatValue = testObj; // Change this line
const shirtValue = testObj; // Change this line
2021-06-15 00:49:18 -07:00
```
# --solutions--
```js
2021-10-27 15:10:57 +00:00
const testObj = {
2021-06-15 00:49:18 -07:00
"hat": "ballcap",
"shirt": "jersey",
"shoes": "cleats"
};
2021-10-27 15:10:57 +00:00
const hatValue = testObj.hat;
const shirtValue = testObj.shirt;
2021-06-15 00:49:18 -07:00
```