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:
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
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 objectIn 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 structuresvalue of captain too
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.I get confused, because I assume they mean thatObjects have property names (likecaptain
is an object withincrew
, but isn't it really a property within a property? Isn't the only object in this codespaceship
?
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
.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?
Wait, I'm explaining it a bit wrong
The key/value pairing is what's considered the
property
And so
spaceship
is only different form the nested objects because it is a variable connected object?So the
yearBuilt
key and 2018
value combined are a property on the telescope
object. My apologies for the incorrect informationSummarized:
Objects ->
{}
Property -> :
What really through me off was that I assumed any new object had to have the =
before the {}
as in spaceship
...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