Accessing Properties Flashcards

1
Q

We can chain operators to access nested properties. We’ll have to pay attention to which operator makes sense to use in each layer. It can be helpful to pretend you are the computer and evaluate each expression from left to right so that each operation starts to feel a little more manageable.

https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/learn-javascript-objects/lessons/objects/exercises/nested-objects

A

spaceship.nanoelectronics[‘back-up’].battery; // Returns ‘Lithium’
In the preceding code:

First the computer evaluates spaceship.nanoelectronics, which results in an object containing the back-up and computer objects.
We accessed the back-up object by appending [‘back-up’].
The back-up object has a battery property, accessed with .battery which returned the value stored there: ‘Lithium’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Dot operator

With property dot notation, we write the object’s name, followed by the dot operator and then the property name (key):

let spaceship = {
  homePlanet: 'Earth',
  color: 'silver'
};
spaceship.homePlanet; // Returns 'Earth',
spaceship.color; // Returns 'silver',
A

Bracket Notation

The second way to access a key’s value is by using bracket notation, [ ].

You’ve used bracket notation when indexing an array:

[‘A’, ‘B’, ‘C’][0]; // Returns ‘A’
To use bracket notation to access an object’s property, we pass in the property name (key) as a string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

With bracket notation you can also use a variable inside the brackets to select the keys of an object. This can be especially helpful when working with functions:

let returnAnyProp = (objectName, propName) => objectName[propName];

returnAnyProp(spaceship, ‘homePlanet’); // Returns ‘Earth’

If we tried to write our returnAnyProp() function with dot notation (objectName.propName) the computer would look for a key of ‘propName’ on our object and not the value of the propName parameter.

A

Property Assignment
Assignment operator:
One of two things can happen with property assignment:

If the property already exists on the object, whatever value it held before will be replaced with the newly assigned value.
If there was no property with that name, a new property will be added to the object.
= to add new key-value pairs to an object or change an existing property.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

We can chain operators to access nested properties. We’ll have to pay attention to which operator makes sense to use in each layer. It can be helpful to pretend you are the computer and evaluate each expression from left to right so that each operation starts to feel a little more manageable.

https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/learn-javascript-objects/lessons/objects/exercises/nested-objects

A

spaceship.nanoelectronics[‘back-up’].battery; // Returns ‘Lithium’
In the preceding code:

First the computer evaluates spaceship.nanoelectronics, which results in an object containing the back-up and computer objects.
We accessed the back-up object by appending [‘back-up’].
The back-up object has a battery property, accessed with .battery which returned the value stored there: ‘Lithium’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly