Understanding object notation - JS newbie

A third installment of my non-understanding of JS, but hey ... here we go. As mentioned before I'm taking codecademy's intro course, and today it's all about objects. This one slide is about nested objects, and the text says: "In application code, objects are often nested— an object might have another object as a property which in turn could have a property that’s an array of even more objects!" And then this code is showned as an example:
const spaceship = {
telescope: {
yearBuilt: 2018,
model: '91031-XLT',
focalLength: 2032
},
crew: {
captain: {
name: 'Sandra',
degree: 'Computer Engineering',
encourageTeam() { console.log('We got this!') }
}
},
};
const spaceship = {
telescope: {
yearBuilt: 2018,
model: '91031-XLT',
focalLength: 2032
},
crew: {
captain: {
name: 'Sandra',
degree: 'Computer Engineering',
encourageTeam() { console.log('We got this!') }
}
},
};
I get confused, because I assume they mean that captain is an object within crew, but isn't it really a property within a property? Isn't the only object in this code spaceship?
11 Replies
Jochem
Jochem2y ago
the outer object spaceship has a property called crew. That property's value is an object, with a property called captain. The value of captain is another object crew's value could also be a string, number, boolean, or function type, but in this case it's an object
13eck
13eck2y ago
In your code there are threefour objects: 1) The value of spaceship is an object 2) The value of spaceship.telescope is an object 3) The value of spaceship.crew is an object 4) The value of spaceship.crew.captain You'll find in JS that objects are ubiquitous (found everywhere) and are the defaults for a lot of data structures
Jochem
Jochem2y ago
value of captain too
Å Marlon G
Å Marlon GOP2y ago
Hi again @jochemm ! Are you enjoying this as much as I am! 🤣 ... and hi to you too Beck! When you say value, do you mean everything within the {}, or do you mean e.g. yearBuilt: 2018 in telescope only.
13eck
13eck2y ago
I get confused, because I assume they mean that captain is an object within crew, but isn't it really a property within a property? Isn't the only object in this code spaceship?
Objects have property names (like telescope is a property name on the spaceship object) as well as a value. As an example, the telescope object has a property of yearBuilt with a value of 2018.
Å Marlon G
Å Marlon GOP2y ago
Aah ... Ok. So for me to learn how to read code, if there is a word (name) followed by {}, then that is an object because of the curly braces, while properties always have : between the key and value?
13eck
13eck2y ago
Wait, I'm explaining it a bit wrong The key/value pairing is what's considered the property
Å Marlon G
Å Marlon GOP2y ago
And so spaceship is only different form the nested objects because it is a variable connected object?
13eck
13eck2y ago
So the yearBuilt key and 2018 value combined are a property on the telescope object. My apologies for the incorrect information
Å Marlon G
Å Marlon GOP2y ago
Summarized: Objects -> {} Property -> : What really through me off was that I assumed any new object had to have the = before the {} as in spaceship ...
13eck
13eck2y ago
The = is the assignment operator. It means "the stuff after is stored in a variable named by the stuff before"
The assignment (=) operator is used to assign a value to a variable. The assignment operation evaluates to the assigned value.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment
An object initializer is a comma-delimited list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}).
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer
Want results from more Discord servers?
Add your server