2021-02-06 04:42:36 +00:00
---
id: 56533eb9ac21ba0edf2244c7
2021-03-08 07:28:46 -07:00
title: Accede a propiedades de objetos con notación de puntos
2021-02-06 04:42:36 +00:00
challengeType: 1
videoUrl: 'https://scrimba.com/c/cGryJs8'
forumTopicId: 16164
dashedName: accessing-object-properties-with-dot-notation
---
# --description--
2021-03-08 07:28:46 -07:00
Hay dos maneras de acceder a las propiedades de un objeto: notación de puntos (`.` ) y notación de corchetes (`[]` ), similar a un arreglo.
2021-02-06 04:42:36 +00:00
2021-03-08 07:28:46 -07:00
La notación de puntos es lo que se usa cuando conoces el nombre de la propiedad a la que intentas acceder con antelación.
2021-02-06 04:42:36 +00:00
2021-03-08 07:28:46 -07:00
Aquí hay un ejemplo de cómo usar la notación de puntos (`.` ) para leer la propiedad de un objeto:
2021-02-06 04:42:36 +00:00
```js
2021-10-27 15:10:57 +00:00
const myObj = {
2021-02-06 04:42:36 +00:00
prop1: "val1",
prop2: "val2"
};
2021-10-27 15:10:57 +00:00
const prop1val = myObj.prop1;
const prop2val = myObj.prop2;
2021-02-06 04:42:36 +00:00
```
2021-03-08 07:28:46 -07:00
`prop1val` tendrá una cadena con valor `val1` y `prop2val` tendrá una cadena con valor `val2` .
2021-10-27 15:10:57 +00:00
2021-02-06 04:42:36 +00:00
# --instructions--
2021-03-08 07:28:46 -07:00
Lee los valores de las propiedades de `testObj` utilizando la notación de puntos. Asigna la variable `hatValue` igual a la propiedad `hat` del objeto y asigna la variable `shirtValue` igual a la propiedad `shirt` del objeto.
2021-02-06 04:42:36 +00:00
# --hints--
2021-03-08 07:28:46 -07:00
`hatValue` debe ser una cadena
2021-02-06 04:42:36 +00:00
```js
assert(typeof hatValue === 'string');
```
2021-03-08 07:28:46 -07:00
El valor de `hatValue` debe ser la cadena `ballcap`
2021-02-06 04:42:36 +00:00
```js
assert(hatValue === 'ballcap');
```
2021-03-08 07:28:46 -07:00
`shirtValue` debe ser una cadena
2021-02-06 04:42:36 +00:00
```js
assert(typeof shirtValue === 'string');
```
2021-03-08 07:28:46 -07:00
El valor de `shirtValue` debe ser la cadena `jersey`
2021-02-06 04:42:36 +00:00
```js
assert(shirtValue === 'jersey');
```
2021-03-08 07:28:46 -07:00
Debes usar la notación de puntos dos veces
2021-02-06 04:42:36 +00: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-02-06 04:42:36 +00: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-02-06 04:42:36 +00:00
```
# --solutions--
```js
2021-10-27 15:10:57 +00:00
const testObj = {
2021-02-06 04:42:36 +00:00
"hat": "ballcap",
"shirt": "jersey",
"shoes": "cleats"
};
2021-10-27 15:10:57 +00:00
const hatValue = testObj.hat;
const shirtValue = testObj.shirt;
2021-02-06 04:42:36 +00:00
```